在本文中,我们将演示如何使用进度条小部件。 进度条与其他小部件的不同之处在于,它会及时更新。
QT4 进度条示例
让我们从代码开始:
#! /usr/bin/env python# -*- coding: utf-8 -*-#import sysfrom PyQt4.QtGui import *from PyQt4.QtCore import *from PyQt4.QtCore import pyqtSlot,SIGNAL,SLOTclass QProgBar(QProgressBar):value = 0@pyqtSlot()def increaseValue(progressBar):progressBar.setValue(progressBar.value)progressBar.value = progressBar.value+1# Create an PyQT4 application object.a = QApplication(sys.argv)# The QWidget widget is the base class of all user interface objects in PyQt4.w = QWidget()# Set window size.w.resize(320, 240)# Set window titlew.setWindowTitle("PyQT4 Progressbar @ pythonspot.com ")# Create progressBar.bar = QProgBar(w)bar.resize(320,50)bar.setValue(0)bar.move(0,20)# create timer for progressBartimer = QTimer()bar.connect(timer,SIGNAL("timeout()"),bar,SLOT("increaseValue()"))timer.start(400)# Show windoww.show()sys.exit(a.exec_())
实例bar(QProgBar类的)用于保存进度条的值。 我们调用函数setValue()来更新其值。 给定参数w将其附加到主窗口。 然后,将其移动到屏幕上的位置(0, 20),并为其指定宽度和高度。
为了及时更新进度条,我们需要一个QTimer()。 我们将小部件与计时器连接起来,计时器将调用函数gainValue()。 我们将计时器设置为每 400 毫秒重复一次函数调用。 您还会看到单词SLOT和SIGNAL。 如果用户执行诸如单击按钮,在框中键入文本之类的操作,则该小部件会发出信号。 该信号没有任何作用,但可用于连接一个槽,该槽充当接收器并对其起作用。
结果:

PyQT 进度条
