python pyqt5 QTimer创建读秒器,提供更新和停止方法
效果图:
代码:
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel
from PyQt5.QtCore import QTimer
import sys
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.label = QLabel("0", self)
self.label.setGeometry(50, 50, 50, 50)
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_time)
self.timer.start(1000)
def update_time(self):
current_time = int(self.label.text())
self.label.setText(str(current_time + 1))
def stop_timer(self):
self.timer.stop()
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec_())