from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import sys import time
class myTimer(QThread): signal=pyqtSignal(str)#string类型信号 def __init__(self): super(myTimer,self).__init__() def run(self):
#发射信号‘1’ self.signal.emit("1") time.sleep(1 )
#self.signal.emit(“2”) #可多次发射信号
class MyDialog(QDialog): def __init__(self, parent=None): super().__init__(parent) self.button = QPushButton(self) self.button.setText('button') self.button.clicked.connect(self._click_do_something)#点击事件 @staticmethod def _show_message(message): print(message) def _click_do_something(self): self.timer = myTimer(1) self.timer.signal.connect(self._show_message)#信号与show_message绑定,timer emit 信号传递给show_message self.timer.start()#timer.run()
if __name__ == '__main__': app = QApplication(sys.argv) dialog = MyDialog() dialog.show() sys.exit(app.exec_())