pyqt5线程龟兔赛跑
哈哈哈
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file '龟兔赛跑.ui' # # Created by: PyQt5 UI code generator 5.15.9 # # WARNING: Any manual changes made to this file will be lost when pyuic5 is # run again. Do not edit this file unless you know what you are doing. from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtCore import QThread, pyqtSignal class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName("MainWindow") MainWindow.resize(800, 600) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.label = QtWidgets.QLabel(self.centralwidget) self.label.setGeometry(QtCore.QRect(180, 120, 131, 31)) self.label.setObjectName("label") self.label_2 = QtWidgets.QLabel(self.centralwidget) self.label_2.setGeometry(QtCore.QRect(440, 120, 131, 31)) self.label_2.setObjectName("label_2") self.textEdit = QtWidgets.QTextEdit(self.centralwidget) self.textEdit.setGeometry(QtCore.QRect(90, 160, 241, 201)) self.textEdit.setObjectName("textEdit") self.textEdit_2 = QtWidgets.QTextEdit(self.centralwidget) self.textEdit_2.setGeometry(QtCore.QRect(420, 160, 271, 201)) self.textEdit_2.setObjectName("textEdit_2") self.pushButton = QtWidgets.QPushButton(self.centralwidget) self.pushButton.setGeometry(QtCore.QRect(310, 420, 111, 61)) self.pushButton.setObjectName("pushButton") MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 23)) self.menubar.setObjectName("menubar") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) # 创建兔子和乌龟的对象,把对象的信号连接槽函数,槽函数追加文本到面板 self.r = Rabbit() # 把兔子实例的sinOut信号连接到rabbit槽函数, self.r.sinOut.connect(self.rabbit) self.t = Tortoise() self.t.sinOut.connect(self.tortoise) self.pushButton.clicked.connect(self.start) def start(self): self.r.start() self.t.start() # rabbit方法,它是一个槽函数,用于处理接收到的信号。将传入的字符串 str 追加到 # self.textEdit 控件中 def rabbit(self,str): self.textEdit.setPlainText(self.textEdit.toPlainText() + str) def tortoise(self,str): self.textEdit_2.setPlainText(self.textEdit_2.toPlainText() + str) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.label.setText(_translate("MainWindow", "兔子的比赛记录")) self.label_2.setText(_translate("MainWindow", "乌龟的比赛记录")) self.pushButton.setText(_translate("MainWindow", "开始比赛")) class Rabbit(QThread): # 声明了一个名为 sinOut 的信号,它是一个从 pyqtSignal 类实例化的对象。pyqtSignal是 PyQt 提供的一个特殊类 # 用于创建信号。它是 QObject 类的信号和槽机制的一部分 # 信号可以用来在 PyQt 应用程序的不同组件之间传递信息。 # sinOut是信号的名称,可以在类的其他方法中发射(emit)这个信号,也可以连接到一个槽函数。 sinOut = pyqtSignal(str) # 重写run方法 def __init__(self): super(Rabbit,self).__init__() def run(self): for i in range(1,11): QThread.msleep(100) self.sinOut.emit("\n 兔子跑了"+ str(i) + "0米") # 发射信号,传递字符串 if i == 9: self.sinOut.emit("\n 兔子在睡觉") QThread.sleep(5) if i == 10: self.sinOut.emit("\n 兔子到达终点") # 兔子类,乌龟类继承自QThead,重写run方法。定义信号并发射 class Tortoise(QThread): sinOut = pyqtSignal(str) # 重写run方法 def __init__(self): super(Tortoise, self).__init__() def run(self): for i in range(1,11): QThread.msleep(500) self.sinOut.emit("\n 乌龟跑了"+ str(i) + "0米") if i == 10: self.sinOut.emit("\n 乌龟到达终点") import sys # 主方法,程序从此处启动PyQt设计的窗体 if __name__ == '__main__': app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() # 创建窗体对象 ui = Ui_MainWindow() # 创建PyQt设计的窗体对象 ui.setupUi(MainWindow) # 调用PyQt窗体的方法对窗体对象进行初始化设置 MainWindow.show() # 显示窗体 sys.exit(app.exec_()) # 程序关闭时退出进程