PyQT5线程基础(2)
- 线程案例
- 案例一
- 案例二
线程案例
案例一
案例一代码通过线程实现点击按钮向线程传输地址,程序等待20秒后,返回结果。
通过QtDesigner创建如下图所示的界面ui,并用UIC工具转成对应的py文件。
Ui_test文件如下:
from PyQt5 import QtCore, QtGui, QtWidgetsclass Ui_MainWindow(object):def setupUi(self, MainWindow):MainWindow.setObjectName("MainWindow")MainWindow.resize(448, 325)self.centralwidget = QtWidgets.QWidget(MainWindow)self.centralwidget.setObjectName("centralwidget")self.psb_start = QtWidgets.QPushButton(self.centralwidget)self.psb_start.setGeometry(QtCore.QRect(110, 120, 191, 81))self.psb_start.setObjectName("psb_start")MainWindow.setCentralWidget(self.centralwidget)self.menubar = QtWidgets.QMenuBar(MainWindow)self.menubar.setGeometry(QtCore.QRect(0, 0, 448, 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)def retranslateUi(self, MainWindow):_translate = QtCore.QCoreApplication.translateMainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))self.psb_start.setText(_translate("MainWindow", "开始"))
main文件,其中代码如下:
from Ui_test import Ui_MainWindow
from PyQt5.QtWidgets import *
import sys
import time
from threads import MyThreadclass MainWindow(QMainWindow, Ui_MainWindow):def __init__(self, parent=None):super(MainWindow, self).__init__(parent)self.setupUi(self)self.psb_start.clicked.connect(self.button_start)def button_start(self):print("button_start clicked")self.psb_start.setChecked(True)self.psb_start.setDisabled(True)self.th = MyThread(ip='192.168.1.1',port=4000)self.th.finishSignal.connect(self.button_finish)self.th.start()def button_finish(self,msg):print("msg:{}".format(msg))# time.sleep(20)self.psb_start.setDisabled(False)self.psb_start.setChecked(False)if __name__ == '__main__':app = QApplication(sys.argv)window = MainWindow()window.show()sys.exit(app.exec_())
threads文件,其中代码如下:
import time
from PyQt5.QtCore import *class MyThread(QThread):finishSignal = pyqtSignal(str)def __init__(self, ip, port, parent=None):super(MyThread, self).__init__(parent)self.ip = ipself.port = portdef run(self):print('======sleep========ip:{},port{}'.format(self.ip, self.port))time.sleep(20)self.finishSignal.emit('this is a test')return
案例二
案例二通过线程实现用按钮控制进度条。
通过QtDesigner创建如下图所示的界面ui。
mainwindow文件,其中代码如下:
from PyQt5 import QtCore,QtWidgets,QtGui
from PyQt5 import uic
import sys,timeclass MyWindow(QtWidgets.QMainWindow):def __init__(self):QtWidgets.QMainWindow.__init__(self)self.ui = uic.loadUi(r"E:\chap\ex\test_thread\ex03\ex03.ui",self)self.resize(888,200)self.thread ={}self.pushButton.clicked.connect(self.start_work1)self.pushButton_2.clicked.connect(self.start_work2)self.pushButton_3.clicked.connect(self.start_work3)self.pushButton_4.clicked.connect(self.stop_work1)self.pushButton_5.clicked.connect(self.stop_work2)self.pushButton_6.clicked.connect(self.stop_work3)def start_work1(self):self.thread[1] = MyThread(parent=None,index=1)self.thread[1].start()self.thread[1].any_singal.connect(self.my_function)self.pushButton.setEnabled(False)def start_work2(self):self.thread[2] = MyThread(parent=None,index=2)self.thread[2].start()self.thread[2].any_singal.connect(self.my_function)self.pushButton_2.setEnabled(False)def start_work3(self):self.thread[3] = MyThread(parent=None,index=3)self.thread[3].start()self.thread[3].any_singal.connect(self.my_function)self.pushButton_3.setEnabled(False)def stop_work1(self):self.thread[1].stop()self.pushButton.setEnabled(True)def stop_work2(self):self.thread[2].stop()self.pushButton_2.setEnabled(True)def stop_work3(self):self.thread[3].stop()self.pushButton_3.setEnabled(True)def my_function(self,count):cnt = countindex = self.sender().indexif index == 1:self.progressBar.setValue(cnt)if index == 2:self.progressBar_2.setValue(cnt)if index == 3:self.progressBar_3.setValue(cnt)class MyThread(QtCore.QThread):any_singal = QtCore.pyqtSignal(int)def __init__(self, parent=None, index=0):super(MyThread, self).__init__(parent)self.index = indexself.is_runing = Truedef run(self):print("开始线程",self.index)cnt = 0while True:cnt +=1if cnt ==99:cnt=0time.sleep(0.01)self.any_singal.emit(cnt)def stop(self):self.is_runing = Falseprint("结束线程",self.index)self.terminate()app = QtWidgets.QApplication(sys.argv)
mainwindow = MyWindow()
mainwindow.show()
sys.exit(app.exec_())