PDA

View Full Version : [SOLVED] glib.timeout_add not running



jesuisbenjamin
April 18th, 2011, 03:37 PM
Hello,

i'm trying the glib.timeout_add() function (see doc over here (http://developer.gnome.org/pygobject/stable/glib-functions.html#function-glib--timeout-add)). But i can't seem to get the loop started:


import glib

class Thing():
def sayhi(self):
print "hello"

def repeat(self):
glib.timeout_add(3000, self.sayhi)

if __name__ == '__main__':
thing = Thing()
thing.repeat()

Any idea how i should do this?
Thanks
Benjamin

simeon87
April 18th, 2011, 05:19 PM
It will only work when GTK is running. The following code should work but note that it will print the text only once (after 3 seconds), not repeatedly yet:


import glib
import gtk

class Thing():
def sayhi(self):
print "hello"

def repeat(self):
glib.timeout_add(3000, self.sayhi)

if __name__ == '__main__':
thing = Thing()
thing.repeat()
gtk.main()

jesuisbenjamin
April 18th, 2011, 05:24 PM
That's a first step, thanks :)

jesuisbenjamin
April 18th, 2011, 08:11 PM
OK i found the solution:

The timeout will only repeat if the callback returns True. The following code works:


import glib
import gtk

class main():
def sayhi(self):
print "hello"
return True

def repeat(self):
print "here!"
glib.timeout_add(3000, self.sayhi)


if __name__ == '__main__':
thing = main()
thing.repeat()
gtk.main()