[PyQt] pyqt4 problem with label.setText timing within loops

Phil Thompson phil at riverbankcomputing.com
Tue Feb 16 09:47:46 GMT 2010

On Tue, 16 Feb 2010 00:12:47 -0500, Dan Cherry <dan.s.cherry at gmail.com>
> I'm trying to run a simple 'for' loop to execute some external programs 
> (ripping tracks with cdparanoia/lame, but I've stripped the problem down 
> to bare essentials that demonstrate the problem).
> Each loop should update a gui label with the current loop number.  The
> setText statement is within the 'for' loop, but the gui doesn't get
> updated until the loop is either completed or several iterations into
> the complete loop.  The loop works, but the updates to the  label do not.
> The Gui was created with QTDesigner, and I'm using python 2.6.4 on
> Ubuntu 9.10.
> The statements that are crucial follow below, and I've attached the two
> py files if you care to see the full code.
> Any suggestions for getting the label.setText statements to update the 
> gui with each loop iteration, would be appreciated.
> TIA,
> Dan Cherry
>      def runloop(self):
>          self.ui.label.setText('Begin Loop') ## <--doesn't update before 
> loop
>          for i in range(1,6):
>              self.ui.label.setText('loop %s' % i) ## <--doesn't update 
> for each loop - but does update after the loop completes
>              rval = self.loop(i)
>      def loop(self, loop_no):
>          time.sleep(3)
>          print loop_no  ## <--the print statements display properly as 
> the loop progresses
>          return

The GUI will only get updated when the event loop runs. Call
QCoreApplication.processEvents() after setText().


More information about the PyQt mailing list