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. hihoCoder #1695 公平分队II

    题目大意 Alice 和 Bob 在玩一个游戏.Alice 将 $1$ 到 $2n$ 这 $2n$ 个整数分成两组,每组 $n$ 个.Bob 从中选一组,剩下一组归 Alice.Alice 可以与 B ...

  2. Spring 注解大全与详解

    Spring使用的注解大全和解释 注解 解释 @Controller 组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类 ...

  3. Codechef APRIL14 ANUCBC Cards, bags and coins 背包DP变形

    题目大意 有n个数字,选出一个子集,有q个询问,求子集和模m等于0的方案数%1000000009.(n <= 100000,m <= 100,q <= 30) 假设数据很小,我们完全 ...

  4. JavaScript设计模式与开发实践——读书笔记1.高阶函数(下)

    上部分主要介绍高阶函数的常见形式,本部分将着重介绍高阶函数的高级应用. 1.currying currying指的是函数柯里化,又称部分求值.一个currying的函数会先接受一些参数,但不立即求值, ...

  5. UESTC 2015dp专题 E 菲波拉契数制 dp

    菲波拉契数制 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/65 Descr ...

  6. 2015 UESTC 搜索专题J题 全都是秋实大哥 kmp

    全都是秋实大哥 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/contest/show/61 Desc ...

  7. bzoj 1303: [CQOI2009]中位数图 数学

    1303: [CQOI2009]中位数图 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/ ...

  8. Windows UWP开发系列 – 3D变换

    在Win8.1中,引入了一个PlaneProjection可以实现3D变换,但它的变换方式比较简单,只能实现基本的旋转操作.在Windows 10 UWP中,引入了一个更加强大的3D变换Transfo ...

  9. Git 中文件的状态和流转区

    Git的文件主要处于三种状态,分别是 staged, modified, committed. Git文件流转有三个区域,分别是 工作区域. 索引区域. 本地数据区域. 要修改对一个文件进行操作,首先 ...

  10. 【spring】在spring cloud项目中使用@ControllerAdvice做自定义异常拦截,无效 解决原因

    之前在spring boot服务中使用@ControllerAdvice做自定义异常拦截,完全没有问题!!! GitHub源码地址: 但是现在在spring cloud中使用@ControllerAd ...