from gi.repository import GObject def handler(): print("Hello world") # Create a new Gtk main loop mainloop = GObject.MainLoop() # Add a function to be called when the main loop is idle GObject.idle_add(handler) # Run the main loop mainloop.run()
from gi.repository import GObject def handler(): print("Hello world") return True # True to keep handler connected # Create a new Gtk main loop mainloop = GObject.MainLoop() # Add a function to be called every second timeout_id = GObject.timeout_add_seconds(1, handler) # Run the main loop mainloop.run()This example creates a new Gtk main loop using the GObject.MainLoop class. It then adds a function to be called every second using the GObject.timeout_add_seconds() function. The function returns True so that it will continue to be called until it is disconnected. Finally, it runs the main loop using the mainloop.run() function. Both examples use the GObject package provided by the gi.repository package.