Pygtk, widget refreshing

John Hunter jdhunter at ace.bsd.uchicago.edu
Tue Sep 23 22:25:13 CEST 2003


>>>>> "Arne" == Arne Schwabe <arne at rfc2549.org> writes:

    Arne> hi, I am using python and pygtk.  In my program I want to
    Arne> realize a status thing with a DrawingArea.  But my problem
    Arne> is that this area is not updated while my program is
    Arne> running.  Is there a way I can force pygtk to refresh to
    Arne> widgets?

Two good ways, one is to use a timer and the other an idle.  A timer
will call your function of choice every so many milliseconds, and an
idle func will do it whenever the gtk event loop is idle

For the timer

    def update_widget(*args):
       pass  # do something here

    gtk.timeout_add(250, update_widget)  # call update_widget every 250 ms

For the idle func, you can pass an arg that will be passed to your
idle func

   gtk.idle_add(update_widget) 


Here is how I update a status bar -- this may not be ideal, it's just
what I came up with when I confronted it the first time

Create the status bar

    self.statbar = gtk.Statusbar()
    self.statbar.show()
    self.statbarCID = self.statbar.get_context_id('my stat bar')
    self.vbox.pack_end(self.statbar)
    self.timeoutId  = gtk.timeout_add(5000,self.update_status_bar)

where update_status_bar looks like

    def update_status_bar(self, msg=None):
        if msg is None:
            # generate default message
            
        try: self.statbarCID, self.statbarMID
        except AttributeError: pass
        else: self.statbar.remove(self.statbarCID, self.statbarMID)
            
        self.statbarMID = self.statbar.push(self.statbarCID, msg)
        return gtk.TRUE

and then later when the dialog is destroyed

    gtk.timeout_remove(self.timeoutId)

Hope this helps,
John Hunter    

        






More information about the Python-list mailing list