In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "instance usage of PyQt5 pyqt multithreading". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
First of all, let's look at an example:
# coding=utf-8 _ _ author__ = 'a359680405' from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * global sec sec=0 def setTime (): global sec sec+=1 lcdNumber.display (sec) # LED display number + 1 def work (): timer.start (1000) # timer count per second for i in range (2000000000): pass timer.stop () app=QApplication ([]) top=QWidget () layout=QVBoxLayout (top) # Vertical layout Class QVBoxLayout LcdNumber=QLCDNumber () # add a display layout.addWidget (lcdNumber) button=QPushButton ("test") layout.addWidget (button) timer=QTimer () timer.timeout.connect (setTime) # trigger setTime button.clicked.connect (work) top.show () app.exec ()
Our main interface has a LCD digital panel to display the time and a button to start the task. The purpose of the program is for the user to click a button to start a very time-consuming operation (in the program we replace this time-consuming work with a cycle of 2000000000 times, in a real program, this may be a network access, it may need to copy a large file or other task), while LCD starts to display the number of elapsed milliseconds. The number of milliseconds is updated through a timer QTimer. When the calculation is complete, the timer stops. This is a very simple application, and I don't see any problem. But when we started running the program, there was a problem: after clicking the button, the program interface stopped responding directly and did not start updating until the end of the loop, so the timer used to display 0.
Experienced developers are quick to point out that threads are needed here. This is because all interfaces in Qt are in the UI thread (also known as the main thread, that is, the thread that executed QApplication::exec ()), and performing time-consuming operations in this thread (such as that loop) blocks the UI thread and causes the interface to stop responding. When the interface stops responding, the user experience is naturally not good, but more seriously, some window managers will detect that your program has become unresponsive and may advise the user to forcibly stop the program, so your program may be terminated. the task can no longer be completed. So, to avoid this problem, we need to start a new thread using QThread:
# coding=utf-8 _ _ author__ = 'a359680405' from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * global sec sec=0 class WorkThread (QThread): trigger = pyqtSignal () def _ int__ (self): super (WorkThread) Self). _ init__ () def run (self): for i in range (203300030): pass self.trigger.emit () # signal def countTime (): global sec sec+=1 lcdNumber.display (sec) # LED display digits + 1 def work (): timer.start (1000) # timer count per second workThread.start () # timer on Start workThread.trigger.connect (timeStop) # when you get the signal that the cycle is over Stop counting def timeStop (): timer.stop () print ("end of run time", lcdNumber.value () global sec sec=0 app=QApplication ([]) top=QWidget () layout=QVBoxLayout (top) # Vertical layout class QVBoxLayout LcdNumber=QLCDNumber () # add a display layout.addWidget (lcdNumber) button=QPushButton ("test") layout.addWidget (button) timer=QTimer () workThread=WorkThread () button.clicked.connect (work) timer.timeout.connect (countTime) # each time ends, trigger setTime top.show () app.exec ()
I added a WorkerThread class. WorkerThread inherits from the QThread class and overrides its run () function. You can think of the run () function as the code that the new thread needs to execute. This is where the loop is executed, and then a signal is sent that the calculation is complete. In the slot function clicked by the button, start a thread using the workThread.start () function in work () (note that this is not the run () function). Run the program again and you will find that the interface is no longer blocked.
This is the end of the introduction to the example usage of PyQt5 pyqt multithreading. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.