QtGui.QProgressBar

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

时间: 2024-08-13 00:05:39

QtGui.QProgressBar的相关文章

pyqt4.1

# -*- coding: utf-8 -*- import sip  #程序打包需要 import  decimal #程序打包需要 from PyQt4 import QtCore, QtGui from PyQt4 import QtWebKit from PyQt4.QtGui import * from PyQt4.QtCore import * import sys from PyQt4.QtWebKit import QWebView, QWebPage, QWebSettings

pyqt练习x3.21

#!/usr/bin/env python2 "A web browser that will never exceed 128 lines of code. (not counting blanks)" import sys, os, json, tempfile from PyQt4 import QtGui, QtCore, QtWebKit, QtNetwork settings = QtCore.QSettings("ralsina", "dev

ZetCode PyQt4 tutorial widgets I

#!/usr/bin/python # -*- coding: utf-8 -*- """ ZetCode PyQt4 tutorial In this example, a QtGui.QCheckBox widget is used to toggle the title of a window. author: Jan Bodnar website: zetcode.com last edited: September 2011 """ i

各种进度条的使用

说明:代码来自开源中国,所有权归原作者所有 代码: 1 #!/usr/bin/env python 2 # --*--codig: utf8 --*-- 3 4 from PyQt4 import QtGui 5 from PyQt4 import QtCore 6 7 class BaseProgressDialog(QtGui.QWidget): 8 updateProgress = QtCore.pyqtSignal(str) 9 def __init__(self, text='', p

Pyqt 加smtplib实现发送邮件

一. smtplib 的介绍 smtplib.SMTP([host[, port[, local_hostname[, timeout]]]])   SMTP类构造函数,表示与SMTP服务器之间的连接,通过这个连接可以向smtp服务器发送指令,执行相关操作(如:登陆.发送邮件).所有参数都是可选的. host:smtp服务器主机名 port:smtp服务的端口,默认是25:如果在创建SMTP对象的时候提供了这两个参数,在初始化的时候会自动调用connect方法去连接服务器.   smtplib模

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

QProgressBar() 进度条 QProgressBar简介 QProgressBar小部件提供了一个水平或垂直的进度条. 进度条用于向用户指示操作的进度,并向他们保证应用程序仍在运行. 进度条使用steps的概念.您可以通过指定最小和最大可能的step值来设置它,并且当您稍后将当前step值赋给它时,它将显示已经完成的step的百分比.百分比是通过将进度 (value() - minimum()) / (maximum() - minimum())来计算的. 您可以使用setMinimu

QT中可以用QProgressBar或着QProgressDialog来实现进度条

QProgressBar的使用 首先在designer中拖一个按钮和进度条部件,按下面初始化 //补充:下面两句写在MainWindow的构造函数里进行初始化 ui->progressBar->setRange(0,50000-1);  ui->progressBar->setValue(0); 按钮的事件处理: void MainWindow::on_pushButton_clicked() {   for(int i=0;i<50000;i++) { for(int j=

【2017-06-17】QtGui基础控件:QSpinBox及QDoubleSpinBox

今天开始一个新的系列,主要是翻译并摘录QtGui中基础空间的常用方法,并做一些简单的实验程序: 我觉得这是一个炒冷饭的行为,但有时候冷饭不能不炒,不热不好吃,而且也很容易发霉. 其实到现在这种状态,对控件所提供的方法是否熟练已经不是问题,因为我们可以经常去看Manual,更为重要的是程序的框架和数据结构,如何把数据有效组织起来,如何组合或自定义控件来实现我们想要的功能,可能更是一种能力的体现,编程嘛,主要是来解决实际问题的. 希望能通过这个系列,我能对Qt的控件.实现方法有一个更深入的理解. Q

QtGui.QPixmap

A QtGui.QPixmap is one of the widgets used to work with images. It is optimized for showing images on screen. In our code example, we will use the QtGui.QPixmap to display an image on the window. #!/usr/bin/python # -*- coding: utf-8 -*- """