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

最新文章

  1. Javascript格式化json返回的时间(/Date(1482747413000)/)
  2. 在VMware中安装ubuntu出现菜单栏无法显示的情况
  3. static在C中有了第二种含义:用来表示不能被其它文件访问的全局变量和函数
  4. Android UX & UI 最佳实践: 设计有效的导航
  5. Flex Excel下载
  6. 动画(Animation) 、 高级动画(Core Animation)
  7. 005 Python的数值类型
  8. mysql-distinct去重、mysql-group …
  9. TextBox控件只允许输入出生日期,并验证年龄不得小于18岁
  10. 谈谈浏览器http缓存
  11. BZOJ 1041 [HAOI2008]圆上的整点:数学
  12. 【55】java异常机制剖析
  13. 详解mybatis配置文件
  14. Redis在Linux中安装使用
  15. HBase学习笔记2 - HBase shell常用命令
  16. 【PS技巧】如何校正倾斜的图片
  17. RobotFramework--环境安装1
  18. 大数据开发实战:Stream SQL实时开发三
  19. Redis安装和主要功能简介
  20. Python 将一个时间戳格式化为(格林威治时间或者本地时区时间)

热门文章

  1. 一文了解JVM全部垃圾回收器,从Serial到ZGC
  2. Linux6.9用RPM方式安装MySQL5.7.21
  3. C++虚函数、虚继承
  4. 将DLL挂接到远程进程之中(远程注入)
  5. Appium+python自动化18-brew、carthage和appium-doctor
  6. Java 3D游戏引擎——JME(java Monkey Engine)
  7. ios修改textField的placeholder的字体颜色、大小
  8. iOS开源项目:asi-http-request
  9. VisualStudio使用GIT
  10. Android宝典入门篇-基础知识