In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to use multithreading in Python to maintain the response of GUI. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.
Improve button control over the progress bar
Let's improve it now:
The first button is used to launch the progress bar
The second button stops the progress bar
The third button resets the progress bar
In the previous MainWindow class, we set three methods for the events of the progress bar and button:
Let's modify the above three methods and rewrite them into four methods:
Start_progressbar (self): used to launch the progress bar
Stop_progressbar (self): used to stop the progress bar
Reset_progressbar (self): used to reset the progress bar
Update_progressbar (self): used to bind three buttons to the above three methods
Run the program to see the effect:
When we click the "start" button, the progress bar begins to increase, when we click the "stop button", the progress bar stops increasing, and when we click the "reset" button, the progress bar returns to zero.
Control the growth rate of the progress bar
In the start_progressbar () method that starts the progress bar, we increase the progress bar by 0.0001 at a time through the while loop, in which case the progress bar grows very fast. In fact, we can freeze the progress bar through the sleep () method of the time module to achieve the effect of reducing the growth rate of the progress bar.
Modify the start_progressbar () method as follows:
Let's pause the numerical growth of the progress bar for 0.3 seconds to see the effect:
The growth rate of the progress bar is indeed much slower than before.
Use QThread threads
The QThread object of QtCore in PyQt provides a platform-independent way to manage threads.
The QThread object executes the thread by calling the run () method, which by default starts the event loop by calling the exec () method.
Next, let's demonstrate the use of QThread through an example of a progress bar counter.
We create a new class RunThread () that inherits from QtCore.QThread and define a run () method to start the thread and a stop () method to stop the thread.
First, we define a new signal counter_value by instantiating QtCore.pyqtSignal (int), which will be used to update the progress bar. Then you set some default properties in the RunThread () class.
In the run () method, we accumulate the values of the counters through a while loop and send the values of the accumulated counters through the defined signal counter_value.
In the stop () method, we set the running state of the thread to Fasle, and then use the terminate () method of QThread to ensure that the thread is stopped completely.
Then, to apply this progress bar counter, we also need to add and modify it in the main window class MainWindow ():
Add a progress bar counter method progressbar_counter () to start the thread manager:
Add a progress bar setting method set_progressbar () to set the value of the progress bar:
Modify the startup method of the progress bar, start_progressbar (). Delete the while loop and add the progress bar counter to it (because the while loop increment value has been implemented in the progress bar counter):
Modify the stop method stop_progressbar () of the progress bar and add the stop () method of the thread to stop the thread:
After the modification is completed, we run the program to see the effect:
Through the information printed by the terminal, we can directly see the management and control of the QThread thread.
After reading the above, do you have any further understanding of how to use multithreading in Python to maintain the response of GUI? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.