我正在学习Pyside QProgressBar
MacOSX上。 当我使用QProgressBar喜欢以下,它只表明0%或100%。 如何使QProgressBar顺利? 有没有办法做到这一点?
from PySide.QtGui import QApplication, QProgressBar, QWidget
from PySide.QtCore import QTimer
import time
app = QApplication([])
pbar = QProgressBar()
pbar.setMinimum(0)
pbar.setMaximum(100)
pbar.show()
def drawBar():
global pbar
pbar.update()
t = QTimer()
t.timeout.connect(drawBar)
t.start(100)
for i in range(1,101):
time.sleep(0.1)
pbar.setValue(i)
app.exec_()
摆脱这种代码:
for i in range(1,101): # this won't work, because
time.sleep(0.1) # Qt's event loop can't run while
pbar.setValue(i) # you are forcing the thread to sleep
而是,添加一个全局变量p:
p = 0
并增加它在你的拉杆()函数:
def drawBar():
global pbar
global p
p = p + 1
pbar.setValue(p)
pbar.update()
QPropertyAnimation是易于使用,它的平滑变化为您服务。
animation = QtCore.QPropertyAnimation(pbar, "value")
animation.setDuration(???)
animation.setStartValue(0)
animation.setEndValue(100)
animation.start()
编后:
只是我建议的代码替换pbar.show()和app.exec()之间的一切
下面是完整的代码:
from PyQt5.QtWidgets import QWidget, QProgressBar, QApplication
from PyQt5.QtCore import QTimer, QPropertyAnimation
app = QApplication([])
pbar = QProgressBar()
pbar.setMinimum(0)
pbar.setMaximum(100)
pbar.show()
animation = QPropertyAnimation(pbar, "value")
animation.setDuration(2000)
animation.setStartValue(0)
animation.setEndValue(100)
animation.start()
app.exec_()