Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Case Analysis of updating UI Code with Python Pyqt5 Multithreading

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Python Pyqt5 multi-thread update UI code example analysis, in view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

"

In writing the GUI interface, there are usually buttons that trigger events when clicked.

Such as downloading a file or doing something.

These operations can be time-consuming, and if they do not end in time, the main thread will block

In this way, the interface will have an unresponsive state, so multithreading must be used to solve this problem.

"

Code example

From PyQt5.Qt import (QApplication, QWidget, QPushButton,QThread,QMutex,pyqtSignal) import sysimport time qmut_1 = QMutex () # create thread lock qmut_2 = QMutex () qmut_3 = QMutex () # inherit QThreadclass Thread_1 (QThread): # thread 1 def _ _ init__ (self): super (). _ init__ () def run (self): qmut_1.lock () # locked values = [1,2,3 Print ("= Thread_1 =") for i in values: print ("Thread_1:") I) time.sleep (0.5) # dormant qmut_1.unlock () # unlock class Thread_2 (QThread): # Thread 2 _ signal = pyqtSignal () def _ _ init__ (self): super (). _ init__ () def run (self): # qmut_2.lock () # Lock values = ["a", "b", "c" "d", "e", "f", "g", "h", "I", "j", "k"] print ("= = Thread_2 =") for i in values: print ("Thread_2:" I) time.sleep (0.5) # qmut_2.unlock () # unlock self._signal.emit () class Thread_3 (QThread): # Thread 2 _ signal = pyqtSignal () def _ _ init__ (self): super (). _ init__ () def run (self): qmut_3.lock () # Lock values = ["a" "b", "c", "d", "e", "f", "g", "h", "I", "j", "k"] print ("= = Thread_3 =") for i in values: print ("Thread_3:" I) time.sleep (0.5) qmut_3.unlock () # unlock self._signal.emit () # after execution Release signal class Thread_01 (QThread): # Thread 1 def _ _ init__ (self): super (). _ init__ () def run (self): values = [1, 2, 3, 4, 5] print ("= = Thread_01 =") for i in values: print ("Thread_01:" I) time.sleep (0.5) # dormant class Thread_02 (QThread): # Thread 2 def _ _ init__ (self): super (). _ init__ () def run (self): values = ["a", "b", "c", "d" "e"] print ("= Thread_02 =") for i in values: print ("Thread_02:" I) time.sleep (0.5) class MyWin (QWidget): def _ _ init__ (self): super (). _ init__ () # Button initialization self.btn_01 = QPushButton ('button _ run at a time', self) self.btn_01.move (80 40) self.btn_01.clicked.connect (self.click_01) # bind slot function self.btn_02 = QPushButton ('button _ run once per point', self) self.btn_02.move (80,80) self.btn_02.clicked.connect (self.click_02) # bind slot function self.btn_1 = QPushButton ('button _ thread lock _ multiple clicks Execute (lag)', self) self.btn_1.move (80,120) self.btn_1.clicked.connect (self.click_1) # bind slot function self.btn_2 = QPushButton ('button _ thread lock _ click again after receiving the completion signal', self) self.btn_2.move (80) Self.btn_2.clicked.connect (self.click_2) # bind slot function self.btn_3 = QPushButton ('button _ thread lock _ execute again after receiving the completion signal', self) self.btn_3.move (80 Self.btn_3.clicked.connect (self.click_3) # bind slot function def click_01 (self): self.thread_01 = Thread_01 () # create thread self.thread_01.start () # start thread def click_02 (self): self.thread_02 = Thread_02 () # create thread self. Thread_02.start () # start thread def click_1 (self): self.thread_1 = Thread_1 () # create thread self.thread_1.start () # start thread def click_2 (self): self.btn_2.setEnabled (False) self.thread_2 = Thread_2 () self.thread_2._signal.connect ( Self.set_btn_2) # signal connection If the signal is received, execute the corresponding function self.thread_2.start () def click_3 (self): self.btn_3.setEnabled (False) self.thread_3 = Thread_3 () self.thread_3._signal.connect (self.set_btn_3) # signal connection, if the signal is received Execute the corresponding function self.thread_3.start () def set_btn_2 (self): self.btn_2.setEnabled (True) def set_btn_3 (self): self.btn_3.setEnabled (True) if _ _ name__ = "_ _ main__": app = QApplication (sys.argv) myshow = MyWin () myshow.setWindowTitle ("Multithreading demonstration") Myshow.setMinimumHeight (500) myshow.setMinimumWidth (500) myshow.show () sys.exit (app.exec_ ())

Running result

= Thread_01 =

Thread_01: 1

Thread_01: 2

Thread_01: 3

Thread_01: 4

Thread_01: 5

= Thread_02 =

Thread_02: a

Thread_02: b

Thread_02: c

Thread_02: d

Thread_02: e

= Thread_1 =

Thread_1: 1

Thread_1: 2

Thread_1: 3

Thread_1: 4

Thread_1: 5

Thread_1: 6

Thread_1: 7

Thread_1: 8

Thread_1: 9

= Thread_2 =

Thread_2: a

Thread_1: 10

Thread_2: b

Thread_2: c

Thread_2: d

= Thread_3 =

Thread_3: a

Thread_2: e

Thread_3: b

Thread_2: f

Thread_3: c

Thread_2: g

Thread_3: d

Thread_2: h

Thread_3: e

Thread_2: i

Thread_3: f

Thread_2: j

Thread_3: g

Thread_2: k

Thread_3: h

Running process

On the Python Pyqt5 multithreaded update UI code example analysis questions shared here, I hope the above content can be of some help to you, if you still have a lot of doubts have not been solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report