8

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?

houqp
  • 761
  • 1
  • 8
  • 12

3 Answers3

17

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()
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • Thanks, what's the different between glib.timeout_add and gobject.timeout_add? – houqp Sep 05 '11 at 16:04
  • 3
    @houqp: They are the same function. `gobject` imports `timeout_add` from `glib`. – unutbu Sep 05 '11 at 17:10
  • 1
    @pahnin: To stop the `timeout` function from getting called, have it return `False`. See [the docs](http://www.pygtk.org/pygtk2reference/gobject-functions.html#function-gobject--timeout-add). – unutbu May 31 '12 at 13:07
  • ya I did that but I thought may be there is a function to stop the timer like in javascript settimeout cleartimeout – pahnin May 31 '12 at 13:54
  • 1
    Just what I was looking for? Can someone please mark this answer as correct? – evadeflow May 30 '13 at 20:00
  • I get "PyGIDeprecationWarning: GObject.timeout_add is deprecated; use GLib.timeout_add instead" – NoBugs Feb 22 '21 at 04:51
1

If you're using the new Python GObject Introspection API, you should use GLib.timeout_add().

Note that the documentation seems to be incorrect. It is actually:

timeout_add(interval, function, *user_data, **kwargs)

Here's an example. Note that run is a callable object, but it could be any ordinary function or method.

from gi.repository import GLib

class Runner:
    def __init__(self, num_times):
        self.num_times = num_times
        self.count = 0

    def __call__(self, *args):
        self.count += 1
        print("Periodic timer [{}]: args={}".format(self.count, args))

        return self.count < self.num_times

run = Runner(5)

interval_ms = 1000
GLib.timeout_add(interval_ms, run, 'foo', 123)

loop = GLib.MainLoop()
loop.run()

Output:

$ python3 glib_timeout.py 
Periodic timer [1]: args=('foo', 123)
Periodic timer [2]: args=('foo', 123)
Periodic timer [3]: args=('foo', 123)
Periodic timer [4]: args=('foo', 123)
Periodic timer [5]: args=('foo', 123)
<messages stop but main loop keeps running>
Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

or the simplest test code
// base on Jonathon Reinhart's answer

from gi.repository import GLib

i = 0
def test1(*args):
    global i
    i+=1
    print('test1:', i, args)
    if i<3:
        return True # keep running
    else:
        return False # end timer

# call test1 every 1000ms, until it return False
GLib.timeout_add(1000, test1, 'foo', 123)

loop = GLib.MainLoop() # just for test without UI
loop.run()

outputs:

$ python3 ../test_gtk_timeout.py
test1: 1 ('foo', 123)
test1: 2 ('foo', 123)
test1: 3 ('foo', 123)
yurenchen
  • 1,897
  • 19
  • 17