pyQt5从dict创建按钮连接到具有附加值的函数 [英] pyQt5 create pushbutton from dict connect to a function with add value

查看:59
本文介绍了pyQt5从dict创建按钮连接到具有附加值的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法连接我的按钮.我尝试了不同的方式,但我没有到达.有时我的 GUI 不显示,存放在缓冲区中.有时我有预期的输出 Qt.ConnectionType,而不是方法"或参数 1 具有意外类型 'NoneType'

i have a problem to connect my pushbutton. I tried different way but i don't arrive to. Sometimes my GUI doesn't show up, stocked in the buffer. sometimes i have as output Qt.ConnectionType expected, not 'method' or argument 1 has unexpected type 'NoneType'

此脚本用于从网络下载文件.在 download(self) 中,更多的行被注释掉只是为了尝试我的代码.

This script is to download files from the web. In download(self) more of the lines are commented out just to try there is my code.

class Ui_Qwid(object):
    def setupUi(self, Qwid):
        Qwid.setObjectName("Qwid")
        Qwid.resize(423, 795)
        Qwid.setWindowTitle("Softs de secours")
        self.gridLayoutWidget = QtWidgets.QWidget(Qwid)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(-1, 9, 431, 791))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")


        for key,val in les_soft.items():

            self.btn = QtWidgets.QPushButton(key)
            self.gridLayout.addWidget(self.btn)
            self.btn.clicked.connect(self.download(key))


    def download(self,key):
        print("on passe par la")
        urllib.request.urlretrieve(les_soft[key],key+".exe")


        local_filename = key
        url = les_soft[key]
        # NOTE the stream=True parameter
        r = requests.get(url, stream=True)
        with open(local_filename, 'wb') as f:
            for chunk in r.iter_content(chunk_size=1024): 
                if chunk: # filter out keep-alive new chunks
                    f.write(chunk)

感谢您的时间

推荐答案

当你将一个信号连接到它的槽时,你不应该向它传递求值的函数,而只传递函数的名称.我也建议连接在继承自 QWidget、QMainWindow 等的类环境中.

When you connect a signal to its slot you should not pass it the function evaluated but only the name of the function. I also recommend that the connection be within the class environment that inherits from QWidget, QMainWindow, etc.

这个例子我不能完全重现,但如果我可以概括它,那么创建一个字典来模拟你的数据.

This example I can not reproduce completely, but if I can generalize it, so create a dictionary that simulates your data.

为了获得所需的键,我们使用对象作为源,并使用函数 text() 来获取它.

To obtain the desired key we use the object as a source and use the function text() to obtain it.

sender() 函数获取生成信号的对象,在本例中为您按下的按钮.

The sender() function gets the object that generates the signal, in this case the button you press.

import sys
from PyQt5 import QtCore

from PyQt5 import QtWidgets


class Ui_Qwid(object):
    def setupUi(self, Qwid):
        Qwid.setObjectName("Qwid")
        Qwid.resize(423, 795)
        Qwid.setWindowTitle("Softs de secours")
        self.gridLayoutWidget = QtWidgets.QWidget(Qwid)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(-1, 9, 431, 791))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")


class Widget(QtWidgets.QWidget, Ui_Qwid):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent=parent)
        self.setupUi(self)

        self.les_soft = {'key1': 'url1', 'key2': 'url2', 'key3': 'url3', 'key4': 'url4',
                         'key5': 'key5', 'key6': 'url6', 'key7': 'url7', 'key8': 'url8'}

        for key, val in self.les_soft.items():
            self.btn = QtWidgets.QPushButton(key)
            self.gridLayout.addWidget(self.btn)
            self.btn.clicked.connect(self.download)

    def download(self):
        key = self.sender().text()
        print(key)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())

这篇关于pyQt5从dict创建按钮连接到具有附加值的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆