初學pyqt,折騰了很久才把功能實現,功能是按開始按鈕循環輸出數字在textBrowser控件中,按結束按鈕中止輸出,再按開始按鈕從新開始循環app
技術點與其中遇到的坑
一、遇到的坑:一開始想實現按結束按鈕中止循環,結果按了開始就一直結束不了,開始是在Ui_Dialog裏寫了個函數弄了個標誌位想要結束,結果那個標誌位根本做用不了開始循環的標誌位,因此想到用線程來實現,就是界面這個主線程開始一個循環的子線程,按中止按鈕結束循環的子線程。百度了好多,用threading和QThread均可以實現,重點是要用信號槽機制才能將標識位的值改變。函數
二、將thread中的輸出顯示在界面主線程中的textBrowser中
Ui_Dialog相關代碼ui
self.myThread1.strsin.connect(self.updateText) def updateText(self,text): self.textBrowser.append(text)
myThread相關代碼this
strsin = pyqtSignal(str) self.strsin.emit(str(i))
三、經過信號槽機制使線程中止
Ui_Dialog相關代碼spa
sin = pyqtSignal() self.myThread1 = myThread() self.sin.connect(self.myThread1.stop)
myThread相關代碼線程
def stop(self): self.flag = 0
四、點擊按鈕傳參
實現將按鈕的text傳入函數rest
self.startButton.clicked.connect(lambda:self.startOrstop(self.startButton.text()))
# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'myWindow.ui' # # Created by: PyQt5 UI code generator 5.13.1 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtWidgets from PyQt5.QtCore import QThread,pyqtSignal,QObject from PyQt5.QtWidgets import QApplication,QMainWindow import time,sys class Ui_Dialog(QObject): sin = pyqtSignal() def __init__(self): super(Ui_Dialog,self).__init__() self.myThread1 = myThread() self.sin.connect(self.myThread1.stop) self.myThread1.strsin.connect(self.updateText) def setupUi(self, Dialog): Dialog.setObjectName("Dialog") Dialog.resize(593, 415) self.startButton = QtWidgets.QPushButton(Dialog) self.startButton.setGeometry(QtCore.QRect(410, 80, 75, 23)) self.startButton.setObjectName("pushButton") self.stopButton = QtWidgets.QPushButton(Dialog) self.stopButton.setGeometry(QtCore.QRect(410, 140, 75, 23)) self.stopButton.setObjectName("pushButton_2") self.textBrowser = QtWidgets.QTextBrowser(Dialog) self.textBrowser.setGeometry(QtCore.QRect(50, 120, 256, 192)) self.textBrowser.setObjectName("textBrowser") self.retranslateUi(Dialog) QtCore.QMetaObject.connectSlotsByName(Dialog) self.update_tag = 1 self.startButton.clicked.connect(lambda:self.startOrstop(self.startButton.text())) self.stopButton.clicked.connect(lambda:self.startOrstop(self.stopButton.text())) def retranslateUi(self, Dialog): _translate = QtCore.QCoreApplication.translate Dialog.setWindowTitle(_translate("Dialog", "Dialog")) self.startButton.setText(_translate("Dialog", "start")) self.stopButton.setText(_translate("Dialog", "stop")) def startOrstop(self,text): try: print(text) myThread1 = myThread() self.sin.connect(myThread1.stop) if text =='start': self.update_tag = 0 self.textBrowser.clear() self.myThread1.flag = 1 self.myThread1.start() elif text == 'stop': self.update_tag = 1 self.sin.emit() except Exception as e: print(e) def updateText(self,text): self.textBrowser.append(text) class myThread(QThread): strsin = pyqtSignal(str) def __init__(self,parent=None): super(myThread,self).__init__(parent) self.flag = 1 def run(self): try: for i in range(100): if self.flag == 1: self.strsin.emit(str(i)) time.sleep(0.1) QtWidgets.QApplication.processEvents() elif self.flag == 0: self.strsin.emit("stop thread") break self.strsin.emit("please restart") except Exception as e: print(e) def stop(self): self.flag = 0 if __name__ == "__main__": app = QApplication(sys.argv) MainWindow = QMainWindow() ui_dialog = Ui_Dialog() ui_dialog.setupUi(MainWindow) MainWindow.show() sys.exit(app.exec_())
功能展現
code