修改程序,点击暂停按钮可以暂停

77 阅读1分钟

import sys from PyQt5.QtWidgets import QApplication, QWidget, QDialog, QPushButton, QLabel from PyQt5.QtCore import pyqtSlot import random import time class MyDialog(QDialog): def init(self, parent=None): super().init(parent) self.setWindowTitle('Start Dialog') self.label = QLabel(self) self.label.setGeometry(50, 50, 200, 30) # @pyqtSlot() def start(self): myString = "" while True: random_num = random.randint(1, 10) print(random_num) time.sleep(1) if random_num < 5: myString += str(random_num) + " " self.label.setText(myString) self.exec_() class MainWindow(QWidget): def init(self): super().init() self.initUI() def initUI(self): self.setGeometry(300, 300, 600, 600) self.setWindowTitle('Main Window') self.startButton = QPushButton('Start', self) self.startButton.clicked.connect(self.startClicked) self.startButton.setGeometry(100, 80, 100, 30) self.startButton = QPushButton('暂停', self) self.startButton.clicked.connect(self.startClicked) self.startButton.setGeometry(200, 80, 100, 30) self.show() def startClicked(self): dialog = MyDialog(self) dialog.start() if name == 'main': app = QApplication(sys.argv) window = MainWindow() sys.exit(app.exec_()),修改程序,点击暂停按钮可以暂停start()函数执行,并且暂停按钮的文字变为继续,点击继续按钮,start()函数再继续执行