Technology Sharing

【PyQt】

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

PyQT5 thread basics (2)

Thread Case

Case number one

The code in Case 1 uses a thread to transmit the address to the thread by clicking a button. The program waits for 20 seconds and then returns the result.
Use QtDesigner to create the interface UI shown in the figure below, and use the UIC tool to convert it into the corresponding py file.
insert image description here
The Ui_test file is as follows:

from PyQt5 import QtCore, QtGui, QtWidgets

class 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.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.psb_start.setText(_translate("MainWindow", "开始"))

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

The main file contains the following code:

from Ui_test import Ui_MainWindow
from PyQt5.QtWidgets import *
import sys
import time
from threads import MyThread

class 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_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

The threads file contains the following code:

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 = ip
        self.port = port

    def run(self):
        print('======sleep========ip:{},port{}'.format(self.ip, self.port))
        time.sleep(20)

        self.finishSignal.emit('this is a test')
        
        return
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

Case 2

Case 2 uses threads to control the progress bar with buttons.
Use QtDesigner to create the interface ui shown in the figure below.
insert image description here

mainwindow file, the code is as follows:

from PyQt5 import QtCore,QtWidgets,QtGui
from PyQt5 import uic
import sys,time

class MyWindow(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.ui = uic.loadUi(r"E:chapextest_threadex03ex03.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 = count
        index = self.sender().index
        if 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 = index
        self.is_runing = True

    def run(self):
        print("开始线程",self.index)
        cnt = 0
        while True:
            cnt +=1
            if cnt ==99:cnt=0
            time.sleep(0.01)
            self.any_singal.emit(cnt)

    def stop(self):
        self.is_runing = False
        print("结束线程",self.index)
        self.terminate()

app = QtWidgets.QApplication(sys.argv)
mainwindow = MyWindow()
mainwindow.show()
sys.exit(app.exec_())
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84