Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periodically call a function in pygtk's main loop

Tags:

python

pygtk

What's the pygtk equivalent for after method in tkinter?

I want to periodically call a function in the main loop.

What is the better way to achieve it?

like image 748
houqp Avatar asked Sep 05 '11 14:09

houqp


1 Answers

Use gobject.timeout_add:

import gobject
gobject.timeout_add(milliseconds, callback)

For example here is a progress bar that uses timeout_add to update the progress (HScale) value:

import gobject
import gtk

class Bar(object):
    def __init__(self,widget):
        self.val=0
        self.scale = gtk.HScale()
        self.scale.set_range(0, 100)
        self.scale.set_update_policy(gtk.UPDATE_CONTINUOUS)
        self.scale.set_value(self.val)
        widget.add(self.scale)
        gobject.timeout_add(100, self.timeout)
    def timeout(self):
        self.val +=1
        self.scale.set_value(self.val)
        return True

if __name__=='__main__':
    win = gtk.Window()
    win.set_default_size(300,50)
    win.connect("destroy", gtk.main_quit)
    bar=Bar(win)
    win.show_all()
    gtk.main()
like image 71
unutbu Avatar answered Sep 22 '22 17:09

unutbu