"Hello World" in Python with GTK+ 3

Gtk.Application with Python and GTK+ 3.0

Playing around with the newly released GTK+ 3.0 in Python, I thought I would share this simple "Hello World" code which uses the new Gtk.Application object.


from gi.repository import Gtk, Gio

class HelloWorldApp(Gtk.Application):
    def __init__(self):
        Gtk.Application.__init__(self, application_id="apps.test.helloworld",
                                 flags=Gio.ApplicationFlags.FLAGS_NONE)
        self.connect("activate", self.on_activate)
        
    def on_activate(self, data=None):
        window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
        window.set_title("Gtk3 Python Example")
        window.set_border_width(24)
        label = Gtk.Label("Hello World!")
        window.add(label)
        window.show_all()
        self.add_window(window)
    
if __name__ == "__main__":
    app = HelloWorldApp()
    app.run(None)

More Information...

  • PyGObject - The new bindings for GObject, GLib, Gio, Gtk, etc. which replace PyGTK. If the import statement above looks new to then you need to read this.
  • GtkApplicaiton - API documentation for the Gtk.Application obejct. The docs are for the C API, but, translating it to Python is pretty straightforward.
Did you enjoy "Hello World" in Python with GTK+ 3? If you would like to help support my work, A donation of a buck or two would be very much appreciated.
blog comments powered by Disqus
Linux Servers on the Cloud IN MINUTES