1

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

2 回答 2

3

摆脱这段代码:

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-09T04:54:16.963 回答
1

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_()
于 2014-07-09T05:08:05.500 回答