PyQt5实现进度条与定时器及子线程同步关联
作者:Sir 发布时间:2023-03-20 14:48:35
标签: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_())
来源:https://mp.weixin.qq.com/s/rO-cl0HWVtHWtIgOzXq9_A


猜你喜欢
- 今天在开发的时候,项目报了一个警告 Duplicate named routes definition ,这里记录一下
- 一、中间件的基本使用在web开发中,中间件起着很重要的作用。比如,身份验证、权限认证、日志记录等。以下就是各框架对中间件的基本使用。1.1
- http://www.cppcns.com/shujuku/mysql/283231.html 也可以参照这个8.0
- import java.util.Scanner; public class regexTest { // 新建类
- 此文刊登在《程序员》2009年5月期:SQL全名是结构化查询语言(Structured Query Language),一直是后台开发者用来
- 监听服务的信号,实现平滑启动,linux信号说明package main import ("context""
- logging模块是Python的一个标准库模块,开发过程中,可以通过该模块,灵活的完成日志的记录。logging模块提供了两种记录日志的方
- 很多人都将<数据库设计范式>作为数据库表结构设计“圣经”,认为只要按照这个范式需求设计,就能让设计出来的表结构足够优化,既能保证
- 言MySQL 8.0 从第一版release 到现在已经走过了4个年头了,8.0版本在功能和代码上做了相当大的改进和重构。和DBA圈子里的朋
- 引言context包比较小,是阅读源码比较理想的一个入手,并且里面也涵盖了许多go设计理念可以学习。go的Context作为go并发方式的一
- 本文为大家分享了vuejs事件中心管理组件间的通信,供大家参考,具体内容如下事件中心这个可以是一个空的全局的Vue实例,其他的组件利用这个实
- 防止用户通过后退按钮重复提交表单 <% response.Buffer=true response.Expires=0 respons
- MySQL在5.1引入了一个rename database操作,但在MySQL5.1.23后又不支持这个命令。可以说是一个实验性的功能,没有
- keras提供简单方便的模型可视化工具,只需一行代码就可以用框图的形式可视化出你搭建的网络结构。对于复杂网络而言,这个工具就是个神器呀。这篇
- 本文实例讲述了python使用webbrowser浏览指定url的方法。分享给大家供大家参考。具体如下:这段代码提示用户输入关键词,通过we
- 如何用表单的方式推送请求的信息?具体见下:<html><head><title>答复用户信息
- 数据库安全性问题一直是围绕着数据库管理员的恶梦,数据库数据的丢失以及数据库被非法用户的侵入使得数据库管理员身心疲惫不堪。围绕数据库的安全性问
- 名称空间名称空间(namespaces):用于存放名字与内存地址绑定关系的地方,是对栈区的划分作用:名称空间可以使栈区中存放相同的名字,从而
- 本文实例讲述了python中引用与复制用法。分享给大家供大家参考。具体分析如下:在python中,任何不可变对象是传值的,而可变对象是传引用
- 某些情况下:我们希望在一个SQL Server下访问另一个sqlserver数据库上的数据,或者访问其他oracle数据库上的数据,要想完成