PyQt5:如何发出信号时传递参数

时间:2019-08-18 09:40:54

标签: python pyqt5

当我单击带有一个参数的按钮时,我想从主类向mythread类发出(名称和年龄),但是当我添加第二个参数时,却没有,

class main(QWidget):
    sig=pyqtSignal(str,int)
    def __init__(self):
        super().__init__()

        vb=QVBoxLayout()
        self.btn=QPushButton('click here')
        self.btn.clicked.connect(self.ff)

        vb.addWidget(self.btn)
        self.setLayout(vb)

        self.worker=worker()
        self.mythread=QThread()

    def ff(self,name,age): # it works with one argument
        try:
            name='PC'
            age=21
            self.worker.moveToThread(self.mythread)
            self.sig.connect(self.worker.func)
            self.sig.emit(name,age)
            self.mythread.start()
            self.worker.signal.connect(self.worker.func)
        except Exception as e:
            print(str(e))

class worker(QObject):
    signal=pyqtSignal(str,int)

    def __init__(self):
        super().__init__()

    def func(self,name,age):
        print(name,age)

0 个答案:

没有答案