datetime:2019/5/21 17:38
author:nzb
进度条
import sys
from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QGroupBox, \
QHBoxLayout, QPushButton, QVBoxLayout, QSpinBox, QLCDNumber
from PyQt5.QtWidgets import QDial, QProgressBar
from PyQt5 import QtGui, QtCore
from PyQt5.QtCore import Qt, QThread, pyqtSignal
import time
class MyThead(QThread):
change_value = pyqtSignal(int)
def run(self):
cnt = 0
while cnt < 100:
cnt += 1
time.sleep(0.3)
self.change_value.emit(cnt)
class UI_demo(QWidget):
"""用户界面"""
def __init__(self):
super().__init__()
self.title = 'PyQt5 ProgressBar'
self.left = 600
self.top = 200
self.width = 350
self.height = 100
self.iconName = '../img/home.ico'
self.initWindow()
def initWindow(self):
self.setWindowIcon(QtGui.QIcon(self.iconName))
self.setGeometry(self.left, self.top, self.width, self.height)
self.setWindowTitle(self.title)
self.initUI()
self.show()
def initUI(self):
"""进度条"""
vbox = QVBoxLayout()
self.progressbar = QProgressBar()
self.progressbar.setMaximum(100)
self.progressbar.setStyleSheet('QProgressBar {border:2px solid grey; border-radius:8px; padding:1px}'
'QProgressBar::chunk {background:green}')
self.progressbar.setTextVisible(False)
vbox.addWidget(self.progressbar)
self.button = QPushButton("run progressbar")
self.button.clicked.connect(self.startProgressbar)
self.button.setStyleSheet('background-color:yellow')
vbox.addWidget(self.button)
self.setLayout(vbox)
def startProgressbar(self):
"""开始进度条"""
self.thread = MyThead()
self.thread.change_value.connect(self.setProgressVal)
self.thread.start()
def setProgressVal(self, val):
"""设置进度条的值"""
self.progressbar.setValue(val)
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = UI_demo()
sys.exit(app.exec_())