开发者

Python PyQt4 progressbar Freezing GUI

开发者 https://www.devze.com 2023-02-28 01:15 出处:网络
the following code counts a bunch of PyQt4 progressbar\'s up tp 99%, I would like for my GUI NOT to freeze as they count up to 99%. I would LOVE to do this without custom classes or functions if it is

the following code counts a bunch of PyQt4 progressbar's up tp 99%, I would like for my GUI NOT to freeze as they count up to 99%. I would LOVE to do this without custom classes or functions if it is possible. I know it is good to use classes but for this tiny snippet of code I don't want to create a class. From what I've read there may be a update() function that could accomplish this ... please advise If I'm on the right t开发者_开发技巧rack

import sys
import time
from PyQt4 import QtGui
from PyQt4 import QtCore


app = QtGui.QApplication(sys.argv)

widget = QtGui.QWidget()
widget.resize(400, 200)
widget.setWindowTitle('simple')
widget.show()

shift = 0
cntControl = 5
barra = [0] * cntControl
for i in range(cntControl):
    shift = shift + 10
    barra[i] = QtGui.QProgressBar(widget)
    barra[i].show()
    barra[i].setMinimum(0)
    barra[i].setMaximum(10000)
    barra[i].setGeometry(QtCore.QRect(10, shift, 200, 10))


for a in range(10000):
    for i in range(cntControl):
        barra[i].setValue(a)



sys.exit(app.exec_())


try changing your for loop with:

while True:
    for a in range(10000):
        time.sleep(0.0001)
        for i in range(cntControl):
            barra[i].setValue(a)

if works for me. The while loop continues endlessly moving the bar. If you are looking only to clean the bar after it reaches the end you should use reset:

PySide.QtGui.QProgressBar.reset()
Reset the progress bar. The progress bar “rewinds” and shows no
progress

Update after OP comments: If you want your gui to be responsive when entering a long loop or other operation you should use either python thread module or QThreads.


I really could not get threads to work at all ... I could post my thread attempt which looks flawless to my (now tired) eyes ...

I have however been able to tweek http://zetcode.com/tutorials/pyqt4/widgets/ 's progressbar example and came out with the following code ... which solves the problem of freezing in the GUI:

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore


class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.setWindowTitle('ProgressBar')
        self.setGeometry(300, 300, 250, 150)

        self.pbar = [0] * 3
        self.timer = [0] * 3
        self.step = [0] * 3
        self.shift = 0
        for i in range(3):
            self.shift = self.shift + 30
            self.pbar[i] = QtGui.QProgressBar(self)
            self.pbar[i].setGeometry(30, self.shift, 200, 25)

            self.timer[i] = QtCore.QBasicTimer()
            self.step[i] = 0
            self.timer[i].start(100, self)


    def timerEvent(self, event):
        for i in range(3):
            if self.step[i] >= 100:
                self.timer[i].stop()
                return
            self.step[i] = self.step[i] + 1
            self.pbar[i].setValue(self.step[i])




app = QtGui.QApplication(sys.argv) ex
= Example() ex.show() app.exec_()

I have NO IDEA why it works which is probably not a good thing. I'm guessing it might have something to do with super(Example, self).__init__() and the custom timer pyqt4 uses. I was really hoping to do this without functions or classes, but not sure that is possible. If you think it is, feel free to post!

0

精彩评论

暂无评论...
验证码 换一张
取 消