我正在MacOSX上学习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_()发布于 2014-07-09 04:54:16
去掉这段代码:
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并在drawBar()函数中增加它:
def drawBar():
global pbar
global p
p = p + 1
pbar.setValue(p)
pbar.update()发布于 2014-07-09 05:08:05
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_()https://stackoverflow.com/questions/24645585
复制相似问题