PYQt5的回合进度条定时器延时
Timer delay in round proress bar of PYQt5
在PYQT5中,我想通过使用PyQt5 90分钟,在每10分钟后增加UI中的圆形进度条的进度。
我尝试了 2 种方法,第一种是 time.sleep(),第二种是 Qtimer。
第一种方法“time.sleep”
import sys
import os
from datetime import time
import qdarkstyle
from ui_main import *
progress_val = 0
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# APPLY QDARKSTYLE THEME
self.setStyleSheet(qdarkstyle.load_stylesheet_pyside2())
# SET PROGRESS BAR VALUE
self.ui.progressBar.rpb_setMaximum(420)
# SET PROGRESS BAR STYLE
self.ui.progressBar.rpb_setBarStyle('Donet')
# SET PROGRESS BAR LINE COLOR
self.ui.progressBar.rpb_setLineColor((0, 170, 255)) # ARGUMENT RGB AS A TUPLE
# CHANGING THE PATH COLOR
self.ui.progressBar.rpb_setPathColor((255, 30, 99))
# SET PROGRESS BAR TEXT COLOR
self.ui.progressBar.rpb_setTextColor((233, 30, 99)) # ARGUMENT RGB AS A TUPLE
# SET PROGRESS BAR STARTING POSITION
# North, East, West, South
self.ui.progressBar.rpb_setInitialPos('West') # WEST AS STARTING POSITION.
# SET PROGRESS BAR TEXT TYPE : VALUE OR PERCENTAGE
# Value, Percentage
self.ui.progressBar.rpb_setTextFormat('Percentage')
# SET PROGRESS BAR FONT
self.ui.progressBar.rpb_setTextFont('Arial')
# TEXT HIDDEN
self.ui.progressBar.rpb_enableText(False)
# SET PROGRESS BAR LINE WIDTH
self.ui.progressBar.rpb_setLineWidth(10)
# PATH WIDTH
self.ui.progressBar.rpb_setPathWidth(15)
# SET PROGRESS BAR LINE CAP
# RoundCap, SquareCap
self.ui.progressBar.rpb_setLineCap('RoundCap')
# LINE STYLE
# DotLine, DashLine
self.ui.progressBar.rpb_setLineStyle('DotLine')
#######################################################################
## SHOW ==> MAIN WINDOW
########################################################################
self.show()
## == #
# ANIMATE THE PROGRESS
# LETS ADD TIMER TO CHANGE PROGRESSES
# self.timer = QtCore.QTimer()
# self.timer.timeout.connect(self.progress) # progress function
# self.timer.start(60000)
#
# # Change all progresses to zero on start
# QtCore.QTimer.singleShot(0, lambda: self.ui.progressBar.rpb_setValue(0))
def progress(self):
global progress_val
# Set progress values
self.ui.progressBar.rpb_setValue(progress_val)
time.sleep(60)
# Reset progresses if the maximum value is reached
if progress_val > 420:
progress_val = 0;
# Increase value every 60 ms
progress_val += 1
########################################################################
## EXECUTE APP
########################################################################
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
########################################################################
## END===>
########################################################################
输出:
在第一种方法中,我使用了 timer.sleep() 但 UI 卡住了。
第二种方法“QTimer”
import sys
import os
from datetime import time
import qdarkstyle
from ui_main import *
progress_val = 0
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# APPLY QDARKSTYLE THEME
self.setStyleSheet(qdarkstyle.load_stylesheet_pyside2())
# SET PROGRESS BAR VALUE
self.ui.progressBar.rpb_setMaximum(420)
# SET PROGRESS BAR STYLE
self.ui.progressBar.rpb_setBarStyle('Donet')
# SET PROGRESS BAR LINE COLOR
self.ui.progressBar.rpb_setLineColor((0, 170, 255)) # ARGUMENT RGB AS A TUPLE
# CHANGING THE PATH COLOR
self.ui.progressBar.rpb_setPathColor((255, 30, 99))
# SET PROGRESS BAR TEXT COLOR
self.ui.progressBar.rpb_setTextColor((233, 30, 99)) # ARGUMENT RGB AS A TUPLE
# SET PROGRESS BAR STARTING POSITION
# North, East, West, South
self.ui.progressBar.rpb_setInitialPos('West') # WEST AS STARTING POSITION.
# SET PROGRESS BAR TEXT TYPE : VALUE OR PERCENTAGE
# Value, Percentage
self.ui.progressBar.rpb_setTextFormat('Percentage')
# SET PROGRESS BAR FONT
self.ui.progressBar.rpb_setTextFont('Arial')
# TEXT HIDDEN
self.ui.progressBar.rpb_enableText(False)
# SET PROGRESS BAR LINE WIDTH
self.ui.progressBar.rpb_setLineWidth(10)
# PATH WIDTH
self.ui.progressBar.rpb_setPathWidth(15)
# SET PROGRESS BAR LINE CAP
# RoundCap, SquareCap
self.ui.progressBar.rpb_setLineCap('RoundCap')
# LINE STYLE
# DotLine, DashLine
self.ui.progressBar.rpb_setLineStyle('DotLine')
#######################################################################
## SHOW ==> MAIN WINDOW
########################################################################
self.show()
## == #
# ANIMATE THE PROGRESS
# LETS ADD TIMER TO CHANGE PROGRESSES
# self.timer = QtCore.QTimer()
# self.timer.timeout.connect(self.progress) # progress function
# self.timer.start(60000)
#
# # Change all progresses to zero on start
# QtCore.QTimer.singleShot(0, lambda: self.ui.progressBar.rpb_setValue(0))
def progress(self):
global progress_val
# Set progress values
self.ui.progressBar.rpb_setValue(progress_val)
# time.sleep(60)
# Reset progresses if the maximum value is reached
if progress_val > 420:
progress_val = 0;
# Increase value every 60 ms
progress_val += 1
########################################################################
## EXECUTE APP
########################################################################
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
########################################################################
## END===>
########################################################################
实施后,代码没有在预期的时间内取得进展。例如,我给的 1 分钟意味着 timer.start(6000) 但进度条需要超过 1 分钟才能增加。
ui_main.py
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2extn.RoundProgressBar import roundProgressBar
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(502, 266)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.progressBarContainer = QFrame(self.centralwidget)
self.progressBarContainer.setObjectName(u"progressBarContainer")
self.progressBarContainer.setFrameShape(QFrame.StyledPanel)
self.progressBarContainer.setFrameShadow(QFrame.Raised)
self.verticalLayout_2 = QVBoxLayout(self.progressBarContainer)
self.verticalLayout_2.setObjectName(u"verticalLayout_2")
self.progressBar = roundProgressBar(self.progressBarContainer)
self.progressBar.setObjectName(u"progressBar")
self.progressBar.setMinimumSize(QSize(200, 200))
self.progressBar.setMaximumSize(QSize(200, 200))
self.verticalLayout_2.addWidget(self.progressBar, 0, Qt.AlignHCenter|Qt.AlignVCenter)
self.verticalLayout.addWidget(self.progressBarContainer)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
我认为您使用 QTimer 的想法是正确的,但我只是将定时器用于它的定时信号发射器,并使用 time.time()
.[=12 根据系统时钟更新进度条=]
像这样:
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.start_time = time.time()
...
self.show()
self.timer = QTimer()
self.timer.timeout.connect(self.progress)
self.timer.start(1000) # this will emit every second
def progress():
if time.time() - self.start_time >= 420: # this would be 420 seconds
self.start_time = time.time()
self.ui.progressBar.rpb_setValue(time.time() - self.start_time)
...
在PYQT5中,我想通过使用PyQt5 90分钟,在每10分钟后增加UI中的圆形进度条的进度。 我尝试了 2 种方法,第一种是 time.sleep(),第二种是 Qtimer。
第一种方法“time.sleep”
import sys
import os
from datetime import time
import qdarkstyle
from ui_main import *
progress_val = 0
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# APPLY QDARKSTYLE THEME
self.setStyleSheet(qdarkstyle.load_stylesheet_pyside2())
# SET PROGRESS BAR VALUE
self.ui.progressBar.rpb_setMaximum(420)
# SET PROGRESS BAR STYLE
self.ui.progressBar.rpb_setBarStyle('Donet')
# SET PROGRESS BAR LINE COLOR
self.ui.progressBar.rpb_setLineColor((0, 170, 255)) # ARGUMENT RGB AS A TUPLE
# CHANGING THE PATH COLOR
self.ui.progressBar.rpb_setPathColor((255, 30, 99))
# SET PROGRESS BAR TEXT COLOR
self.ui.progressBar.rpb_setTextColor((233, 30, 99)) # ARGUMENT RGB AS A TUPLE
# SET PROGRESS BAR STARTING POSITION
# North, East, West, South
self.ui.progressBar.rpb_setInitialPos('West') # WEST AS STARTING POSITION.
# SET PROGRESS BAR TEXT TYPE : VALUE OR PERCENTAGE
# Value, Percentage
self.ui.progressBar.rpb_setTextFormat('Percentage')
# SET PROGRESS BAR FONT
self.ui.progressBar.rpb_setTextFont('Arial')
# TEXT HIDDEN
self.ui.progressBar.rpb_enableText(False)
# SET PROGRESS BAR LINE WIDTH
self.ui.progressBar.rpb_setLineWidth(10)
# PATH WIDTH
self.ui.progressBar.rpb_setPathWidth(15)
# SET PROGRESS BAR LINE CAP
# RoundCap, SquareCap
self.ui.progressBar.rpb_setLineCap('RoundCap')
# LINE STYLE
# DotLine, DashLine
self.ui.progressBar.rpb_setLineStyle('DotLine')
#######################################################################
## SHOW ==> MAIN WINDOW
########################################################################
self.show()
## == #
# ANIMATE THE PROGRESS
# LETS ADD TIMER TO CHANGE PROGRESSES
# self.timer = QtCore.QTimer()
# self.timer.timeout.connect(self.progress) # progress function
# self.timer.start(60000)
#
# # Change all progresses to zero on start
# QtCore.QTimer.singleShot(0, lambda: self.ui.progressBar.rpb_setValue(0))
def progress(self):
global progress_val
# Set progress values
self.ui.progressBar.rpb_setValue(progress_val)
time.sleep(60)
# Reset progresses if the maximum value is reached
if progress_val > 420:
progress_val = 0;
# Increase value every 60 ms
progress_val += 1
########################################################################
## EXECUTE APP
########################################################################
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
########################################################################
## END===>
########################################################################
输出:
在第一种方法中,我使用了 timer.sleep() 但 UI 卡住了。
第二种方法“QTimer”
import sys
import os
from datetime import time
import qdarkstyle
from ui_main import *
progress_val = 0
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# APPLY QDARKSTYLE THEME
self.setStyleSheet(qdarkstyle.load_stylesheet_pyside2())
# SET PROGRESS BAR VALUE
self.ui.progressBar.rpb_setMaximum(420)
# SET PROGRESS BAR STYLE
self.ui.progressBar.rpb_setBarStyle('Donet')
# SET PROGRESS BAR LINE COLOR
self.ui.progressBar.rpb_setLineColor((0, 170, 255)) # ARGUMENT RGB AS A TUPLE
# CHANGING THE PATH COLOR
self.ui.progressBar.rpb_setPathColor((255, 30, 99))
# SET PROGRESS BAR TEXT COLOR
self.ui.progressBar.rpb_setTextColor((233, 30, 99)) # ARGUMENT RGB AS A TUPLE
# SET PROGRESS BAR STARTING POSITION
# North, East, West, South
self.ui.progressBar.rpb_setInitialPos('West') # WEST AS STARTING POSITION.
# SET PROGRESS BAR TEXT TYPE : VALUE OR PERCENTAGE
# Value, Percentage
self.ui.progressBar.rpb_setTextFormat('Percentage')
# SET PROGRESS BAR FONT
self.ui.progressBar.rpb_setTextFont('Arial')
# TEXT HIDDEN
self.ui.progressBar.rpb_enableText(False)
# SET PROGRESS BAR LINE WIDTH
self.ui.progressBar.rpb_setLineWidth(10)
# PATH WIDTH
self.ui.progressBar.rpb_setPathWidth(15)
# SET PROGRESS BAR LINE CAP
# RoundCap, SquareCap
self.ui.progressBar.rpb_setLineCap('RoundCap')
# LINE STYLE
# DotLine, DashLine
self.ui.progressBar.rpb_setLineStyle('DotLine')
#######################################################################
## SHOW ==> MAIN WINDOW
########################################################################
self.show()
## == #
# ANIMATE THE PROGRESS
# LETS ADD TIMER TO CHANGE PROGRESSES
# self.timer = QtCore.QTimer()
# self.timer.timeout.connect(self.progress) # progress function
# self.timer.start(60000)
#
# # Change all progresses to zero on start
# QtCore.QTimer.singleShot(0, lambda: self.ui.progressBar.rpb_setValue(0))
def progress(self):
global progress_val
# Set progress values
self.ui.progressBar.rpb_setValue(progress_val)
# time.sleep(60)
# Reset progresses if the maximum value is reached
if progress_val > 420:
progress_val = 0;
# Increase value every 60 ms
progress_val += 1
########################################################################
## EXECUTE APP
########################################################################
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())
########################################################################
## END===>
########################################################################
实施后,代码没有在预期的时间内取得进展。例如,我给的 1 分钟意味着 timer.start(6000) 但进度条需要超过 1 分钟才能增加。
ui_main.py
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
from PySide2extn.RoundProgressBar import roundProgressBar
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
if not MainWindow.objectName():
MainWindow.setObjectName(u"MainWindow")
MainWindow.resize(502, 266)
self.centralwidget = QWidget(MainWindow)
self.centralwidget.setObjectName(u"centralwidget")
self.verticalLayout = QVBoxLayout(self.centralwidget)
self.verticalLayout.setObjectName(u"verticalLayout")
self.progressBarContainer = QFrame(self.centralwidget)
self.progressBarContainer.setObjectName(u"progressBarContainer")
self.progressBarContainer.setFrameShape(QFrame.StyledPanel)
self.progressBarContainer.setFrameShadow(QFrame.Raised)
self.verticalLayout_2 = QVBoxLayout(self.progressBarContainer)
self.verticalLayout_2.setObjectName(u"verticalLayout_2")
self.progressBar = roundProgressBar(self.progressBarContainer)
self.progressBar.setObjectName(u"progressBar")
self.progressBar.setMinimumSize(QSize(200, 200))
self.progressBar.setMaximumSize(QSize(200, 200))
self.verticalLayout_2.addWidget(self.progressBar, 0, Qt.AlignHCenter|Qt.AlignVCenter)
self.verticalLayout.addWidget(self.progressBarContainer)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"MainWindow", None))
我认为您使用 QTimer 的想法是正确的,但我只是将定时器用于它的定时信号发射器,并使用 time.time()
.[=12 根据系统时钟更新进度条=]
像这样:
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
self.start_time = time.time()
...
self.show()
self.timer = QTimer()
self.timer.timeout.connect(self.progress)
self.timer.start(1000) # this will emit every second
def progress():
if time.time() - self.start_time >= 420: # this would be 420 seconds
self.start_time = time.time()
self.ui.progressBar.rpb_setValue(time.time() - self.start_time)
...