Search code examples
pythongtkpygobject

Gtk label not updated in timer script


I'm working on my first Python and GTK script. I'm trying to do a counter/timer. The problem I have is that, while the logging function returns the proper values every second, the gtk.label is not updated. What am I doing wrong?

def startTimer(self, buttonStart):
    self.imgTimer.set_from_stock(Gtk.STOCK_YES, 2)
    self.runTimer(120)

def runTimer(self, timeout):

    for i in reversed(range(0,timeout)):

        logging.debug(i) #returns values 
        self.labelTimer.set_text(i) #doesn't do anything

        time.sleep(1)

Solution

  • You don't give GTK+ a chance to draw the updated the label. You should either use threads (see PyGTK FAQ), or something like

    while gtk.events_pending ():
        gtk.main_iteration ()
    

    after updating the label.