PyQt5实现进度条与定时器及子线程同步关联

 更新时间:2023年01月12日 10:02:25   作者:Sir 老王  
这篇文章主要为大家详细介绍了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开发环境的配置方法

    win7 下搭建sublime的python开发环境的配置方法

    Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text的主要功能包括:拼写检查,书签,完整的 Python API,Goto功能,即时项目切换,多选择,多窗口等等。
    2014-06-06
  • Python操作Elasticsearch详细指南

    Python操作Elasticsearch详细指南

    Elasticsearch 是一种强大且灵活的分布式搜索引擎,而 Python 则以其易用性和强大的数据处理能力,成为开发者在数据操作中的理想选择,下面我们就来看看如何使用Python操作Elasticsearch吧
    2025-02-02
  • python实现凯撒密码加密解密的示例代码

    python实现凯撒密码加密解密的示例代码

    本文主要介绍了python实现凯撒密码加密解密的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-06-06
  • python实现象棋游戏

    python实现象棋游戏

    这篇文章主要为大家详细介绍了python实现象棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-05-05
  • pytorch利用Dataset读取数据报错问题及解决

    pytorch利用Dataset读取数据报错问题及解决

    这篇文章主要介绍了pytorch利用Dataset读取数据报错问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • 利用python3随机生成中文字符的实现方法

    利用python3随机生成中文字符的实现方法

    最近在学习python3,发现网上关于ptyhon3随机生成中文的资料非常少,所以决定将自己实现的方法分享下,下面这篇文章主要给大家介绍了关于利用python3随机生成中文字符的实现方法,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-11-11
  • 在Python的Django框架中创建语言文件

    在Python的Django框架中创建语言文件

    这篇文章主要介绍了在Python的Django框架中创建语言文件的方法,以语言代码来表示语言区域种类,需要的朋友可以参考下
    2015-07-07
  • Python中celery的使用

    Python中celery的使用

    Celery是一个简单、灵活且可靠的,处理大量消息的分布式系统,专注于实时处理的异步任务队列,同时也支持任务调度。接下来通过本文给大家介绍Python中celery的使用详解,感兴趣的朋友一起看看吧
    2021-08-08
  • Python中对象的引用与复制代码示例

    Python中对象的引用与复制代码示例

    这篇文章主要介绍了Python中对象的引用与复制代码示例,具有一定借鉴价值,需要的朋友可以了解下。
    2017-12-12
  • urllib2自定义opener详解

    urllib2自定义opener详解

    这篇文章主要介绍了urllib2自定义opener详解,需要的朋友可以参考下
    2014-02-02

最新评论