• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

PyQt5-进度条实现

武飞扬头像
hansy
帮助2

进度条类控件主要显示任务的执行进度。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>

运行结果

学新通

本篇文章来至:学新通

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通
  • 本文地址: https://www.swvq.com/boutique/detail/tanhcjikhf
  • 联系方式: luke.wu#vfv.cc
系列文章
更多 icon
同类精品
更多 icon
我要评论
我的头像
精彩评论
继续加载