from PyQt5 import QtWidgets,QtGui,QtCore from PyQt5.Qt import QThread,pyqtSignal import sys import time class Thd1(QThread): sig = pyqtSignal(); def __init__(self): super().__init__(); pass def run(self): a = [1, 2, 3, 4, 5]; for i in a: print(i); time.sleep(1); self.sig.emit(); class Thd2(QThread): def __init__(self): super().__init__(); pass def run(self): a = ['a','b','c','d','e']; for i in a: print(i); time.sleep(1); class UI(QtWidgets.QWidget): def SetUi(self): self.setGeometry(300, 300, 300, 220) self.btn1 = QtWidgets.QPushButton("btn1",self); self.btn2 = QtWidgets.QPushButton("btn2", self); self.btn1.clicked.connect(self.click_btn1); self.btn2.clicked.connect(self.click_btn2); self.Vlayout = QtWidgets.QVBoxLayout(); self.Hlayout = QtWidgets.QHBoxLayout(); self.Hlayout.addStretch(1); self.Hlayout.addWidget(self.btn1) self.Hlayout.addWidget(self.btn2) self.Vlayout.addStretch(1); self.Vlayout.addLayout(self.Hlayout); self.setLayout(self.Vlayout); self.show(); def click_btn1(self): self.btn1.setEnabled(False) self.th1 = Thd1(); self.th1.sig.connect(self.enableBtn1) self.th1.start() def click_btn2(self): self.th2 = Thd2(); self.th2.start() def enableBtn1(self): self.btn1.setEnabled(True); if __name__ == '__main__': app = QtWidgets.QApplication([]) ui = UI(); ui.SetUi(); sys.exit(app.exec_());
pyQT 线程与UI通信
最新推荐文章于 2023-10-14 19:44:53 发布