如何在PyQt5中的类之间连接信号

2024-10-05 10:21:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个很常见的问题,但对我来说并不容易。在

我试图将一个PyQt信号从MainWindow类连接到CheckExcel类,但它不起作用。我想有些人知道怎么做,但有些人不知道。大多数不知道的人指出了密码。。。但请不要这样做。我想说的是,我该怎么联系起来呢?在

下面是一些代码:

class CheckExcel(QtCore.QThread):
    updated = QtCore.pyqtSignal(int)
    updateLab = QtCore.pyqtSignal(str)
    running = False

    def __init__(self, parent=None):
        super(CheckExcel, self).__init__(parent)
        self.progPercent = 0
        self.running = True

    def run(self):
        pythoncom.CoInitialize()

        try:
            while self.running == True:

                self.updated.emit(int(self.progPercent))
                self.updateLab.emit(str(va_TC))

                print(self.progPercent)

        except :
            print('Excel is not executed')

            CheckExcel().run()

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent=parent)
        self.setupUi(self)

    def startBtnClicked(self):

        self.chk_excel = CheckExcel(self)
        self.chk_excel.start()

        self.chk_excel.updated.connect(self.updateValue)
        self.chk_excel.updateLab.connect(self.updateLabel)

    def updateValue(self, data):
        self.progressBar.setValue(data)

    def updateLabel(self, text):
        self.label.setText(text)

    def stop(self):
        self.event.set()

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

Tags: selfinitdefexcelrunningclassparentupdated

热门问题