Condivisione della tecnologia

【PyQt】

2024-07-12

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

Nozioni di base sul thread PyQT5 (2)

Caso di filo

Caso numero uno

Il codice nel caso 1 implementa il clic su un pulsante per trasmettere l'indirizzo al thread attraverso il thread. Il programma attende 20 secondi prima di restituire il risultato.
Utilizzare QtDesigner per creare l'interfaccia utente come mostrato nella figura seguente e utilizzare lo strumento UIC per convertirla nel file py corrispondente.
Inserisci qui la descrizione dell'immagine
Il file Ui_test è il seguente:

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

file principale, il codice è il seguente:

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

threads, il codice è il seguente:

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

Caso 2

Il caso 2 implementa l'utilizzo dei pulsanti per controllare la barra di avanzamento attraverso i thread.
Utilizzare QtDesigner per creare l'interfaccia utente mostrata nella figura seguente.
Inserisci qui la descrizione dell'immagine

file mainwindow, il codice è il seguente:

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