PyQt custom progress bar

1. Custom progress bar control

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_())

Show results:
insert image description here

2. Call the progress bar control

# -*- 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_())

Show results:
insert image description here
insert image description here
insert image description here

Guess you like

Origin blog.csdn.net/qq_38641985/article/details/127788023