[PyKDE] gui update fails

Michael Lara laram at nyu.edu
Mon Feb 19 16:27:42 GMT 2007


Hi everyone

I am using Qt 4.2.2 and PyQt 4.1 on Linux. The mailing list archives show
that this is a rather common question, yet I couldn't find out why the
following code isn't working. I am simply trying to update the GUI as a
thread goes through three stages:

- worker thread hasn't been started
- worker thread is working
- worker thread has finished

In the code below, why doesn't the caption of the QPushButton get updated to
"running..." the moment MyQPushButton.runThread() is invoked? On my system,
the button initially reads "Click to run thread!". Then I click it, the app
prints to stdout 

running...
thread sleeping...
thread finished

and then the button text becomes "Done!". Why isn't the button caption
updated (to "running...") while the thread is sleeping?

Thanks in advance for any advice

Michael

#!/usr/bin/env python

import sys, time

from PyQt4.QtGui import *
from PyQt4.QtCore import *

class Th(QThread):
    
    def run(self):
        print "thread sleeping..."
        time.sleep(2)
        print "thread finished"
        return
    pass

class MyQPushButton(QPushButton):
    
    def __init__(self, caption, parent = None):
        
        QPushButton.__init__(self, caption, parent)
        self.connect(self, SIGNAL("clicked()"), self.runThread)
        return
    
    def runThread(self): 
        
        self.setText("running...") # <<== NEVER HAPPENS?!
        self.update()
        print "running..."
        
        th = Th()
        th.start()
        
        while not th.isFinished():
            
            self.update()         
            time.sleep(0.1)
            pass
        self.setText("Done!")
        return
    pass

app = QApplication(sys.argv)

button = MyQPushButton("Click to run thread!")

button.show()

app.exec_()




More information about the PyQt mailing list