进度条类控件主要显示任务的执行进度。PyQt5主要提供两种类型进度条控件:进度条ProgressBar和滑块控件QSlider。
一、ProgressBar:进度条
Demo
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.uic import loadUi
from PyQt5.QtCore import QBasicTimer
import sys
class MyMainForm(QWidget):
def __init__(self):
super().__init__()
loadUi("Bar.ui", self)
self.slot_init() #初始化槽函数
# 初始化
def slot_init(self):
# 初始化变量
self.timer = QBasicTimer()
self.step = 0
# 初始化函数
self.button.clicked.connect(self.onStart)
def onStart(self):
# 判断进度条是否完成,完成则关闭窗体
if self.button.text() == '完成':
self.close()
# 判断计数器是否处于计数状态
if self.timer.isActive():
self.timer.stop()
self.button.setText('开始')
else:
self.timer.start(100, self)
self.button.setText('停止')
# QBasicTimer的附属函数
def timerEvent(self, e):
if self.step >= 100:
self.timer.stop()
self.button.setText('完成')
return
self.step = self.step+1
self.progressBar.setValue(self.step) # 动态更新计数器的值
# from PyQt5 import QtGui
# value = self.progressBar.value()
# self.progressBar.setFont(QtGui.QFont("楷体", value))
if __name__=='__main__':
app=QApplication(sys.argv)
w=MyMainForm()
w.show()
sys.exit(app.exec_())
对应的ui文件为
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>466</width>
<height>353</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QProgressBar" name="progressBar">
<property name="geometry">
<rect>
<x>100</x>
<y>90</y>
<width>301</width>
<height>31</height>
</rect>
</property>
<property name="value">
<number>0</number>
</property>
</widget>
<widget class="QPushButton" name="button">
<property name="geometry">
<rect>
<x>210</x>
<y>150</y>
<width>51</width>
<height>31</height>
</rect>
</property>
<property name="text">
<string>开始</string>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
运行结果
ProgressBar控件对应PyQt5中的QProgressBar类,常用方法及说明见下表,最常用的信号valueChanged,进度条值发生改变时发射。
注:如果最小值和最大值都设置为0,进度条会显示为一个不断循环滚动的繁忙进度。
二、QSlider:滑块控件
PyQt5中提供两个滑动控件型输入控件QSlider和QScrollBar。
Demo
from PyQt5.QtWidgets import QApplication, QWidget
from PyQt5.uic import loadUi
import sys
class MyMainForm(QWidget):
def __init__(self):
super().__init__()
loadUi("Slider.ui", self)
self.slot_init()
def slot_init(self):
self.slider.valueChanged.connect(self.onStart)
self.slider.setMinimum(0) # 设置最小值
self.slider.setMaximum(100) # 设置最大值
self.slider.setValue(50) # 设置当前值
# # 刻度线
# from PyQt5.QtWidgets import QSlider
# self.slider.setTickPosition(QSlider.TicksBelow)
# self.slider.setTickInterval(5)
def onStart(self):
size=self.slider.value()
print(size)
self.label.setNum(size)
if __name__=='__main__':
app=QApplication(sys.argv)
w=MyMainForm()
w.show()
sys.exit(app.exec_())
对应的ui文件为
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Form</class>
<widget class="QWidget" name="Form">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>452</width>
<height>326</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<widget class="QSlider" name="slider">
<property name="geometry">
<rect>
<x>140</x>
<y>150</y>
<width>160</width>
<height>22</height>
</rect>
</property>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>210</x>
<y>120</y>
<width>41</width>
<height>21</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
运行结果
© 版权声明
文章版权归作者所有,未经允许请勿转载,侵权请联系 admin@trc20.tw 删除。
THE END