PyQt5实现进度条与定时器及子线程同步关联
进度条是当我们处理冗长的任务时使用的控件,它是以动画的形式让用户知道该任务正在取得进展。
在PyQt5中的进度条对应组件是QProgressBar,该对象继承自QWidget组件。一般在生产业务的实现过程中还需要借助定时器来实现的。
在实际使用的过程当中进度条的进度控制往往还要根据子线程的业务执行过程来设置进度显示。

PyQt5在之前的文章中已经多次使用,想必大家都比较熟悉了。若是没有安装的话使用pip的方式安装一下即可。
pip install PyQt5
接下来将本文中使用到的相关模块都导入到代码块中。
# It imports all the classes, methods, and attributes of the `PyQt5.QtWidgets` module. from PyQt5.QtWidgets import * # It imports all the classes, methods, and attributes of the `PyQt5.QtCore` module. from PyQt5.QtCore import * # It imports all the classes, methods, and attributes of the `PyQt5.QtGui` module. from PyQt5.QtGui import * # It imports the `sys` module. import sys # It imports the `time` module and it renames it to `t`. import time as t
接下来我们创建一个ProgressBarUI的python类,将所有的进度条的布局以及槽函数放到该类中。
# This class is a widget that displays a progress bar.
class ProgressBarUI(QWidget):
def __init__(self):
super(ProgressBarUI, self).__init__()
self.init_ui()
def init_ui(self):
"""
This function initializes the UI.
"""
self.setWindowTitle('PyQt5进度条案例 公众号:Python 集中营')
self.setWindowIcon(QIcon('prop.png'))
self.resize(500, 200)
self.progressBar = QProgressBar()
self.progressBar.setValue(0)
self.progressBar.setAlignment(Qt.AlignCenter)
hbox = QHBoxLayout()
hbox.addWidget(self.progressBar)
self.timer = QTimer()
self.timer.timeout.connect(self.listen_step)
self.timer.start(100)
self.step = 0
self.thread_ = WorkThread()
self.thread_.thread_step.connect(self.step_add)
self.thread_.start()
self.setLayout(hbox)
def step_add(self, n):
"""
It adds n to the current value of the counter
:param n: the number of steps to take
"""
self.step = self.step + n
def listen_step(self):
"""
> The function `listen_step` is called by the `listen` function, and it returns the next step in the conversation
"""
if self.progressBar.value() >= 100:
self.timer.stop()
else:
self.progressBar.setValue(self.step)
创建一个子线程WorkThread继承自QThread用于处理所有的业务实现逻辑。
# This class is a subclass of QThread and it's used to create a thread that will run the function that will be passed to # it class WorkThread(QThread): # A signal that is emitted when the value of the counter changes. thread_step = pyqtSignal(int) def __init__(self): """ A constructor. It is called when an object is created from a class and it allows the class to initialize the attributes of a class. """ super(WorkThread, self).__init__() self.working = True def __del__(self): """ A destructor. It is called when the object is destroyed. """ self.working = False def run(self): for n in range(1, 101): self.thread_step.emit(1) t.sleep(0.1)
使用python模块的main函数,将整个应用加入到系统的主体循环中。
# A common idiom to use this as the last statement in a module (a file that contains Python code). if __name__ == '__main__': app = QApplication(sys.argv) main = ProgressBarUI() main.show() sys.exit(app.exec_())
到此这篇关于PyQt5实现进度条与定时器及子线程同步关联的文章就介绍到这了,更多相关PyQt5同步关联内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
win7 下搭建sublime的python开发环境的配置方法
Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text的主要功能包括:拼写检查,书签,完整的 Python API,Goto功能,即时项目切换,多选择,多窗口等等。2014-06-06


最新评论