Pyqt案例(一)计时器
本例包含内容:自定义信号、多线程、QLCDNumber
简单,适合当范例,忘记时候回来看看
ChatGPT能帮助解读代码
源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| import sys
from PySide6.QtCore import * from PySide6.QtWidgets import *
sec = 0
class TimeThread(QThread): timer = Signal() end = Signal()
def __init__(self): super(TimeThread, self).__init__()
def run(self): while True: self.sleep(1) if sec == 5: self.end.emit() break self.timer.emit()
class Counter(QWidget): def __init__(self): super(Counter, self).__init__()
self.setWindowTitle("计数器") self.resize(300,120)
layout = QVBoxLayout() self.lcd = QLCDNumber() layout.addWidget(self.lcd)
self.butten = QPushButton('开始计数') layout.addWidget(self.butten)
self.setLayout(layout)
self.timeThread = TimeThread()
self.timeThread.timer.connect(self.countTime) self.timeThread.end.connect(self.end) self.butten.clicked.connect(self.workThread)
def workThread(self): self.timeThread.start()
def countTime(self): global sec sec+=1 self.lcd.display(sec)
def end(self): QMessageBox.information(self,'消息','计时结束',QMessageBox.Ok)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = Counter()
window.show()
sys.exit(app.exec())
|
问题
1 2 3 4 5 6 7 8 9
| timer = Signal() self.timeThread.timer.connect(self.countTime)
self.timer = Signal()
AttributeError: 'PySide6.QtCore.Signal' object has no attribute 'connect'
|