[Python] Conflicting tutorials: Should new development use GtkWindow or GtkApplication in...

Discussão em 'Python' iniciado por Stack, Novembro 27, 2025.

  1. Stack

    Stack Membro Participativo

    I have just started learning about creating GUI apps in Python. I decided to use Gtk version 3. According to the (official?) tutorial on The Python GTK+ 3 Tutorial the proper way of building a hello world application is:

    from gi.repository import Gtk

    class MyWindow(Gtk.Window):
    def __init__(self):
    Gtk.Window.__init__(self)
    self.set_default_size(200, 100)
    self.connect('destroy', Gtk.main_quit)
    self.show_all()

    MyWindow()
    Gtk.main()


    In other tutorial ("Hello World" in Python with GTK+ 3) I found completly different aproach which is:

    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)


    Could someone experienced in this category tell me in what way should I write Gtk 3 apps in python these days? I'm already familiar with writing GUIs (spent few months in Java's Swing) so you can go on with terms like events, callbacks and so on..

    Continue reading...

Compartilhe esta Página