내 연락처 정보
우편메소피아@프로톤메일.com
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
사례 1의 코드는 버튼을 클릭하여 스레드를 통해 스레드에 주소를 전송하는 것을 구현합니다. 프로그램은 결과를 반환하기 전에 20초 동안 기다립니다.
QtDesigner를 이용하여 아래 그림과 같이 인터페이스 UI를 생성하고, UIC툴을 이용하여 해당 py파일로 변환합니다.
Ui_test 파일은 다음과 같습니다.
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", "开始"))
기본 파일의 코드는 다음과 같습니다.
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_())
스레드 파일의 코드는 다음과 같습니다.
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
사례 2는 버튼을 사용하여 스레드를 통해 진행률 표시줄을 제어하는 방법을 구현합니다.
QtDesigner를 사용하여 아래 그림과 같은 인터페이스 UI를 만듭니다.
mainwindow 파일의 코드는 다음과 같습니다.
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_())