A progress bar is a widget that is used when we process lengthy tasks. It is animated so that the user knows that the task is progressing. The QtGui.QProgressBar widget provides a horizontal or vertical progress bar in PyQt4 toolkit. The programmer can set the minimum and maximum value for the progress bar. The default values are 0 and 99.

#!/usr/bin/python
# -*- coding: utf-8 -*- """
ZetCode PyQt4 tutorial This example shows a QtGui.QProgressBar widget. author: Jan Bodnar
website: zetcode.com
last edited: September 2011
""" import sys
from PyQt4 import QtGui, QtCore class Example(QtGui.QWidget): def __init__(self):
super(Example, self).__init__() self.initUI() def initUI(self): self.pbar = QtGui.QProgressBar(self)
self.pbar.setGeometry(30, 40, 200, 25) self.btn = QtGui.QPushButton('Start', self)
self.btn.move(40, 80)
self.btn.clicked.connect(self.doAction) self.timer = QtCore.QBasicTimer()
self.step = 0 self.setGeometry(300, 300, 280, 170)
self.setWindowTitle('QtGui.QProgressBar')
self.show() def timerEvent(self, e): if self.step >= 100: self.timer.stop()
self.btn.setText('Finished')
return self.step = self.step + 1
self.pbar.setValue(self.step) def doAction(self): if self.timer.isActive():
self.timer.stop()
self.btn.setText('Start') else:
self.timer.start(100, self)
self.btn.setText('Stop') def main(): app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_()) if __name__ == '__main__':
main()

In our example we have a horizontal progress bar and a push button. The push button starts and stops the progress bar.

self.pbar = QtGui.QProgressBar(self)

This is a QtGui.QProgressBar constructor.

self.timer = QtCore.QBasicTimer()

To activate the progress bar, we use a timer object.

self.timer.start(100, self)

To launch a timer event, we call its start() method. This method has two parameters: the timeout and the object which will receive the events.

def timerEvent(self, e):

    if self.step >= 100:

        self.timer.stop()
self.btn.setText('Finished')
return self.step = self.step + 1
self.pbar.setValue(self.step)

Each QtCore.QObject and its descendants have a timerEvent() event handler. In order to react to timer events, we reimplement the event handler.

def doAction(self):

    if self.timer.isActive():
self.timer.stop()
self.btn.setText('Start') else:
self.timer.start(100, self)
self.btn.setText('Stop')

Inside the doAction() method, we start and stop the timer.

Figure: QtGui.QProgressBar

QtGui.QProgressBar的更多相关文章

  1. PyQt4进度条QProgressBar

    当我们在处理一个好事较长的任务时,可能就会用到进度条部件.因为使用进度条可以形象告诉用户当前的人物正在进行中.PyQt4工具包提供了水平和垂直两种类型的进度条部件.我们可以设置进度条的最大和最小值,默 ...

  2. Pyqt Smtplib实现Qthread多线程发送邮件

    一. smtplib 的介绍 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])   SMTP类构造函数,表示与SMTP服务器之间的连接 ...

  3. pyqt4_应用例子(计算器,对话框,进度条,日历等等)

    sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...

  4. ZetCode PyQt4 tutorial widgets I

    #!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, ...

  5. qt4.8转qt5.4

    1.头文件包含    #include <QtGui/QProgressBar>    #include <QtGui/QProgressDialog>    #include ...

  6. PYQT4 Python GUI 编写与 打包.exe程序

    工作中需要开发一个小工具,简单的UI界面可以很好的提高工具的实用性,由此开启了我的第一次GUI开发之旅,下面将自己学习的心得记录一下,也做为学习笔记吧!!! 参考:http://www.qaulau. ...

  7. PyQt4 进度条和日历 代码

    # -*- coding: utf-8 -*- """ ------------------------------------------------- File Na ...

  8. 读Pyqt4教程,带你入门Pyqt4 _007

    QSlider 滑块是由一个简单的滑柄的窗口组件.该滑柄可以前后拖动,通过这种方式我们可以为特定任务选择值.有时候使用滑块比简单提供数值或使用微调框(spin box)更自然. QLabel 显示文字 ...

  9. 【PyQt5-Qt Designer】QProgressBar() 进度条

    QProgressBar() 进度条 QProgressBar简介 QProgressBar小部件提供了一个水平或垂直的进度条. 进度条用于向用户指示操作的进度,并向他们保证应用程序仍在运行. 进度条 ...

随机推荐

  1. luoguP4366 [Code+#4]最短路 最短路

    好久没写过博客了.... 本题还是挺有趣的(很水的最短路) 关键在于怎么优化这$n^2$条连边 通常,我们希望用一些边来替代一条边从而减小边集 那么,注意到异或操作可以拆分成按位运算,因此我们只需考虑 ...

  2. 51nod2000 四边形分割平面 规律题

    观察样例,$ans(1) = 1, ans(2) = 10$,再手推一组,$ans(3) = 26$ 可以发现规律$ans(n) = (2n - 1)^2 + 1$ 如果还是没看出规律,那么打个程序去 ...

  3. bzoj 1018 线段树维护连通性

    本题将一道LCT的题特殊化(支持加边和删边,询问图的连通性),将图变成了2×m的网格图,然后就神奇地可以用线段树来维护. 对于每个区间[l,r],维护其四个角落之间的连通性(仅仅通过[l,r]这段的边 ...

  4. 使用 dsc_extractor 导出 dyld_shared_cache_arm64

    iOS系统的全部Framework二进制被打包成一个cache文件,位于 /System/Library/Caches/com.apple.dyld 目录下,我们要想查看某个系统库的二进制需要将 dy ...

  5. MariaDB Audit Plugin 1.2

    下载地址:https://downloads.mariadb.com/enterprise/bbfz-atd2/mariadb-audit-plugin/server_audit-1.2.0.tar. ...

  6. WinForm中DataGridView验证单元格输入的是数字

    转载:http://www.cnblogs.com/ganqiyin/archive/2013/02/18/2915491.html 事件:DataGridView验证单元格输入的是数字,DataGr ...

  7. zookeeper选举机制

    在上一篇文章中我们大致浏览了zookeeper的启动过程,并且提到在Zookeeper的启动过程中leader选举是非常重要而且最复杂的一个环节.那么什么是leader选举呢?zookeeper为什么 ...

  8. Apache下error.log文件太大的处理

    偶尔发现Apache下的错误日志非常的大,有4G多,先停止Apache服务的所有进程,最简单就是输命令:net stop apache2.2,然后删除 Apache2/logs/目录下的 error. ...

  9. [翻译] SoundManager 音频管理器

    SoundManager 音频管理器 https://github.com/nicklockwood/SoundManager Purpose SoundManager is a simple cla ...

  10. EventBus (一) 使用详解——初步使用EventBus

    版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[+] 前言:EventBus是上周项目中用到的,网上的文章大都一样,或者过时,有用的没几篇,经过琢磨,请教他人,也终于弄清楚点眉目,记 ...