Skip to content Skip to sidebar Skip to footer

Pygtk Glib.timeout_add(): How To Tell If Timer Not Being Destroyed?

In my application I use a function to show GtkInfoBars with a timeout (as described https://stackoverflow.com/a/1309257/406281) thanks to glib.timeout_add_seconds(). I understand t

Solution 1:

Regarding bar.hide being called more than once, I think that's not happening because, as far as I know, it should be enough to return a value that is evaluated as False and bar.hide returns None which should be enough. However, if you want to make completely sure, you can verify with something like this:

import gtk, glib

defcallback():
    print'called'

glib.timeout_add_seconds(1, callback)
gtk.main()

In this example, the callback returns None as well and is called just once as you can see from the output to stdout.

Regarding your final question, I'd say this is already an acceptable way of having multiple infobars with a separate timer for each one. Please add more information if I'm missing something.

Solution 2:

Widget.hide always returns None, which glib.timeout_add_seconds treats identically to False. Thus, your timer will always terminate after running once. There is a possibility that hide will be called twice (when the user clicks OK and at the 5 s timeout) but that's alright. So yes, your code is fine.

Edit: Just realised something. You're creating a new info bar every time the function runs, then only hiding it. Instead of using hide, use destroy so it gets properly cleaned up.

... OR I need each new InfoBar to replace any previous one (I can't figure out how to do this without inheriting the timer from the previous InfoBar--it gets messy).

It's not too complicated; just reuse the same info bar. Record the return value of glib.timeout_add_seconds, then while replacing the content of the info bar, kill the previous timer with glib.source_remove.

There are usability trade-offs between the two choices you present (clean UI vs. making sure the user doesn't miss important info) but that's another topic and perhaps not that important.

Post a Comment for "Pygtk Glib.timeout_add(): How To Tell If Timer Not Being Destroyed?"