2014-07-09 56 views
1

我在MacOSX上學習Pyside QProgressBar。當我使用如下的QProgressBar時,它只顯示0%或100%。如何順利製作QProgressBar?有沒有辦法做到這一點?如何順利顯示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_() 
+0

爲什麼投了票? –

回答

2

擺脫這個代碼:

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() 
+0

我不知道while循環強制線程進入睡眠狀態。謝謝你提到它。 –

+0

它工作。謝謝。 –

+0

@keimina然後請接受(並且如果可以,請注意上傳) – Schollii

0

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_() 
+0

這對我來說很困難。你能給我整個代碼嗎? –

+0

直列'從PyQt5.QtWidgets從PyQt5.QtCore進口QPropertyAnimation導入QWidget的,QProgressBar,QApplication' 應用程式= QApplication的([]) PBAR = QProgressBar() pbar.setMinimum(0) pbar.setMaximum(100) pbar.show() 動畫= QPropertyAnimation(PBAR, 「值」) animation.setDuration(2000) animation.setStartValue(0) animation.setEndValue(100) animation.start() 應用程序。 exec_() ' – SauloT

+0

評論盒子無法幫助格式化代碼...只需替換pbar.show()和app.exec()之間的所有代碼即可,我建議的代碼爲 – SauloT