PyQt 自定义进度条

37 篇文章 1 订阅

1.自定义进度条控件

import sys
import random
import platform
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import (QCoreApplication, QPropertyAnimation, QDate, QDateTime, QMetaObject, QObject, QPoint, QRect, QSize, QTime, QUrl, Qt, QEvent)
from PyQt5.QtGui import (QBrush, QColor, QConicalGradient, QCursor, QFont, QFontDatabase, QIcon, QKeySequence, QLinearGradient, QPalette, QPainter, QPixmap, QRadialGradient)
from PyQt5.QtWidgets import *

StyleSheet = '''
#BlueProgressBar {
    min-height: 35px; 
    min-width: 120px;
    border: 2px solid #2196F3;/*边框以及边框颜色*/
    border-radius: 5px;
    background-color: #E0E0E0;
}
#BlueProgressBar::chunk {
    min-height: 35px; 
    min-width: 120px;
    background-color: #2196F3;
    width: 12px; /*区块宽度*/
    margin: 0.5px;
}
'''
class ProgressBar(QProgressBar):
    def __init__(self, *args, **kwargs):
        super(ProgressBar, self).__init__(*args, **kwargs)
        self.setValue(0)
        if self.minimum() == self.maximum():
            self.timer = QtCore.QTimer(self, timeout=self.onTimeout)
            self.timer.start(random.randint(1, 3) * 1000)
    def onTimeout(self):
        if self.value() >= 100:
            self.timer.stop()
            self.timer.deleteLater()
            del self.timer
            return
        self.setValue(self.value() + 1)
class SplashScreen(QWidget):
    def __init__(self,parent=None):
        super(SplashScreen,self).__init__(parent) 
        self._parent = parent        
        self.setAttribute(Qt.WA_TranslucentBackground)  # 窗体背景透明
        self.setWindowFlags(Qt.WindowStaysOnTopHint | Qt.FramelessWindowHint | Qt.Tool) 
        self.initUI()
        
    def center(self):
        screen = self._parent
        size = self.geometry()
        self.move(20,20)

    def initUI(self):
        self.circularProgressBarBase = QFrame(self)
        self.circularProgressBarBase.setObjectName(u"circularProgressBarBase")
        self.circularProgressBarBase.setGeometry(QRect(20, 20, 500, 340))
        self.circularProgressBarBase.setFrameShape(QFrame.NoFrame)
        self.circularProgressBarBase.setFrameShadow(QFrame.Raised)
        self.circularProgress = QFrame(self.circularProgressBarBase)
        self.circularProgress.setObjectName(u"circularProgress")
        self.circularProgress.setGeometry(QRect(20, 20, 500, 330))
        self.circularProgress.setStyleSheet(u"QFrame{background-color: qconicalgradient(cx:0.5,cy:0.5,angle:90,stop:0.749 rgba(255,0,127,0),stop:0.750 rgba(85,170,255,255));}")
        self.circularProgress.setFrameShape(QFrame.NoFrame)
        self.circularProgress.setFrameShadow(QFrame.Raised)
        self.circularBg = QFrame(self.circularProgressBarBase)
        self.circularBg.setObjectName(u"circularBg")
        self.circularBg.setGeometry(QRect(20, 20, 500, 330))
        self.circularBg.setStyleSheet(u"QFrame{background-color:rgba(77, 77, 127, 120);}")
        self.circularBg.setFrameShape(QFrame.NoFrame)
        self.circularBg.setFrameShadow(QFrame.Raised)
        self.container = QFrame(self.circularProgressBarBase)
        self.container.setObjectName(u"container")
        self.container.setGeometry(QRect(35, 35, 450, 290))
        self.container.setStyleSheet(u"QFrame{background-color:rgb(77, 77, 127);}")
        self.container.setFrameShape(QFrame.NoFrame)
        self.container.setFrameShadow(QFrame.Raised)
        self.widget = QWidget(self.container)
        self.snap_label = QLabel(self)
        self.snap_label.setAlignment(Qt.AlignCenter)
        self.snap_label.setStyleSheet(u"QFrame{background-color:rgb(127, 177, 227);}")
        image_pix = QPixmap("ico.png").scaled(120,120)
        self.snap_label.setPixmap(image_pix)
        font1 = QFont()
        font1.setFamily(u"Segoe UI")
        font1.setPointSize(18)
        self.label_tip = QLabel("小爱正在为您加载")
        self.label_tip.setFont(font1)
        self.label_tip.setStyleSheet(u"background-color:none;color: #FFFFFF")
        self.label_tip.setAlignment(Qt.AlignCenter)
        font2 = QFont()
        font2.setFamily(u"Segoe UI")
        font2.setPointSize(55)
        self.label_progress = QLabel("%0")
        self.label_progress.setFont(font2)
        self.label_progress.setStyleSheet(u"background-color:none;color: #AFFFFF")
        self.label_progress.setAlignment(Qt.AlignCenter)
        self.progress_label = QLabel("  进度: ")
        self.progress_label.setAlignment(Qt.AlignRight)
        self.progress_label.setStyleSheet(u"background-color:none;color: #FFFFFF;font-size:15px;")
        self.progress_label.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
        self.progress_bar = ProgressBar(self, minimum=0, maximum=100, textVisible=False,objectName="BlueProgressBar")
        self.progress_bar.setStyleSheet(StyleSheet)
        self.progress_bar.setValue(0)

        self.layout1 = QVBoxLayout()
        self.layout1.addWidget(self.label_tip)
        self.layout1.addSpacing(20)
        self.layout1.addWidget(self.label_progress)

        self.layout2 = QHBoxLayout()
        self.layout2.addWidget(self.snap_label)
        self.layout2.addSpacing(55)
        self.layout2.addLayout(self.layout1)

        self.layout3 = QHBoxLayout()
        self.layout3.addWidget(self.progress_label)
        self.layout3.addSpacing(20)
        self.layout3.addWidget(self.progress_bar)

        self.layout4 = QVBoxLayout()
        self.layout4.addSpacing(10)
        self.layout4.addLayout(self.layout2)
        self.layout4.addSpacing(30)
        self.layout4.addLayout(self.layout3)

        self.widget.setLayout(self.layout4)
        self.widget.setObjectName(u"widget")
        self.show()

    def progressBarValue(self, value):
        styleSheet = """
        QFrame{
            background-color: qconicalgradient(cx:0.5, cy:0.5, angle:90, stop:{STOP_1} rgba(255, 0, 127, 0), stop:{STOP_2} rgba(85, 170, 255, 255));
        }
        """
        progress = str(value)+"%"
        self.label_progress.setText(progress)
        self.progress_bar.setValue(value)
       
        progress1 = (100 - value) / 100.0
        stop_1 = str(progress1 - 0.001)
        stop_2 = str(progress1)
        newStylesheet = styleSheet.replace("{STOP_1}", stop_1).replace("{STOP_2}", stop_2)
        self.circularProgress.setStyleSheet(newStylesheet)

    def mousePressEvent(self,event):
        if event.button()==QtCore.Qt.LeftButton:
            self.dragPosition=event.globalPos()-self.frameGeometry().topLeft()
            event.accept()
        if event.button()==QtCore.Qt.RightButton:
            pass
    def mouseMoveEvent(self,event):
        if event.buttons()& QtCore.Qt.LeftButton:
            self.move(event.globalPos()-self.dragPosition)
            event.accept() 
    def mouseDoubleClickEvent(self, event):
        if event.buttons()& QtCore.Qt.LeftButton:
            pass
            #self.close()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = SplashScreen()
    window.show()
    sys.exit(app.exec_())

效果展示:
在这里插入图片描述

2.调用进度条控件

# -*- codeing:utf-8 -*-
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from my_progress import SplashScreen
import random
import sys
import os


class MainView(QWidget):
    def __init__(self,parent=None):
        super(MainView,self).__init__(parent)
        self.setWindowTitle("标注标签查看工具")
        self.resize(1200,800)
        self.progerss_value = 0

        file_label1 = QLabel("文件路径1")
        file_label2 = QLabel("文件路径2")

        file_text1 = QLineEdit("")
        file_text2 = QLineEdit("")


        file_button1 = QPushButton("按钮1")
        file_button2 = QPushButton("按钮2")

        self.progerss = SplashScreen()
        self.progerss.progressBarValue(0)



        start_button = QPushButton("按钮1")
        other_button = QPushButton("其他")





    

        layout1 = QHBoxLayout()
        layout1.addWidget(file_label1)
        layout1.addWidget(file_text1)
        layout1.addWidget(file_button1)

        layout2 = QHBoxLayout()
        layout2.addWidget(file_label2)
        layout2.addWidget(file_text2)
        layout2.addWidget(file_button2)

        layout3 = QHBoxLayout()
        layout3.addWidget(self.progerss)

        layout4 = QHBoxLayout()
        layout4.addWidget(start_button)
        layout4.addWidget(other_button)




        layout = QVBoxLayout()
        layout.addLayout(layout1)
        layout.addLayout(layout2)
        layout.addLayout(layout3)
        layout.addLayout(layout4)

        self.resize(600,500)

        self.timer = QTimer()
        self.timer.timeout.connect(self.progress)
        # TIMER IN MILLISECONDS
        

        self.setLayout(layout)
        start_button.clicked.connect(self.start_cmd)
        self.timer.timeout.connect(self.progress)


    def start_cmd(self):
        self.timer.start(150)

    def progress (self):

        if self.progerss_value > 100:
            self.progerss_value = 0
            self.progerss.progressBarValue(self.progerss_value)
            self.timer.stop()
        else:
            self.progerss.progressBarValue(self.progerss_value)
            self.progerss_value = self.progerss_value + 1
            self.progerss.progressBarValue(self.progerss_value)

            

    
     

if __name__=="__main__":
    app=QApplication(sys.argv)
    windows=MainView()
    windows.show()
    sys.exit(app.exec_())

效果展示:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
PyQt 是一个用于创建GUI应用程序的Python库,可以用于创建各种函数丰富的窗口。要使用PyQt创建带有进度条的弹窗,我们可以按照以下步骤进行操作: 1. 导入PyQt库中的必要模块,比如`QtWidgets`和`QtCore`。 2. 创建一个继承自`QDialog`的自定义窗口类。该类将包含进度条和其他所需的组件。 3. 在自定义窗口类的构造函数中,设置窗口的初始属性,比如标题、大小和布局。 4. 创建一个进度条组件,并将其添加到窗口布局中。 5. 创建一个用于更新进度条的槽函数。槽函数将使用`setValue()`方法来更新进度条的值。 6. 向窗口中添加一个按钮,并将其与槽函数关联。 7. 使用`QThread`类创建一个新的线程,并将其关联到自定义窗口类的槽函数上。 8. 在线程的执行函数中,通过循环和适当的延迟,逐渐增加进度条的值。 9. 运行应用程序主循环,显示弹窗。 下面是一个简单的示例代码,实现了上述步骤中的操作: ``` from PyQt5.QtWidgets import QApplication, QDialog, QProgressBar, QPushButton, QVBoxLayout from PyQt5.QtCore import QThread, pyqtSignal, QTimer class MyDialog(QDialog): def __init__(self): super().__init__() self.setWindowTitle("进度条弹窗") self.resize(300, 100) layout = QVBoxLayout(self) self.progressbar = QProgressBar() layout.addWidget(self.progressbar) button = QPushButton("开始") layout.addWidget(button) button.clicked.connect(self.start_progress) self.timer = QTimer(self) self.timer.timeout.connect(self.update_progress) self.thread = MyThread() def start_progress(self): self.timer.start(100) self.thread.start() def update_progress(self): value = self.progressbar.value() if value < 100: self.progressbar.setValue(value + 10) else: self.timer.stop() class MyThread(QThread): def run(self): for i in range(11): self.msleep(100) self.quit() if __name__ == "__main__": app = QApplication([]) dialog = MyDialog() dialog.show() app.exec_() ``` 以上代码创建了一个简单的弹窗窗口,其中包含一个进度条和一个用于触发进度更新的按钮。单击按钮后,会启动一个新的线程,并在定时器的帮助下逐渐增加进度条的值。当进度条达到100时,定时器停止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值