Simple python GTK4 app template

I’m trying to port the basic example in https://developer.gnome.org/gtk4/unstable/gtk-getting-started.html to python. I was unable to find updated documentation for python GTK4, so it’s trial and error. The part where I’m having trouble now is window creation from app. I’ve tried all commented out variants, some more pythonic, other closer to the C implementation, to no avail. The uncommented one doesn’t raise any exception, but I’m failing to see how it is connecting the window to the app as the C example does. So one question is how to do it. But, more generally, is there a way to infer that from the GIR file or some of these things are implemented in an ad hoc way for the python module?

import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk

def activate(app):
    win = Gtk.ApplicationWindow()
    #win = Gtk.ApplicationWindow(app)
    #win = Gtk.Window(app)
    #win = app.ApplicationWindow()
    #win = app.Window()
    #win = app.application_window()
    #win = app.window()
    #win = app.application_window_new()
    #win = Gtk.application_window_new(app)
    #win = Gtk.gtk_application_window_new(app)
    win.show()

app = Gtk.Application()
app.connect("activate", activate)
app.run()

For basic examples it should be possible to just follow a GTK3 one. The difference is that you call show() instead showAll(). So just ask google for a GTK3 example. I would guess that the Nim examples from the GTK4 book may be a starting point too, but these do not use OOP style.

Well maybe your issue is that your OS does not ship GTK4 yet. Then you would have to wait or install it somehow.

Well maybe your issue is that your OS does not ship GTK4 yet.

It doesn’t but I’ve built it and I’m using that one.

For basic examples it should be possible to just follow a GTK3 one.

Do you mean this one?:

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

(as in 2. Getting Started — Python GTK+ 3 Tutorial 3.4 documentation)

There are many things that fail there, not only show_all. Indeed, I started from that example, and since Gtk.main() failed I assumed I needed an app to run, so I tried to adapt the C example.

There’s an example on the gtk.org web page:

# Load Gtk
import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk

# When the application is launched…
def on_activate(app):
    # … create a new window…
    win = Gtk.ApplicationWindow(application=app)
    # … with a button in it…
    btn = Gtk.Button(label='Hello, World!')
    # … which closes the window when clicked
    btn.connect('clicked', lambda x: win.close())
    win.set_child(btn)
    win.present()

# Create a new application
app = Gtk.Application(application_id='com.example.GtkApplication')
app.connect('activate', on_activate)

# Run the application
app.run(None)

and, of course, there are examples in C to follow.

You need to assign the Gtk.Application instance to any window, in order to let GTK track it, and terminate the application’s instance when the last window is closed.

These are not the constructor’s normal form; constructors don’t have positional arguments, only named ones.

None of the above is even remotely valid.

There’s no Gtk.main() and Gtk.main_quit() any more: the Gtk.Application instance controls the main loop.

In theory, you can reimplement a simple main loop with this:

default_context = GLib.MainContext.get_default()

done = False
window.signal_connect("destroy", lambda w: done = True)

while not done:
    default_context.iteration(True)

but there’s really no point in that.

3 Likes

No, what I remembered was indeed what Mr. Bassi posted, with the app style layout but without classes.

Note that some Widgets behave differently in GTK4, for example the dialogs. The run() function is gone in GTK4. I would assume that the basic program shape of the Nim examples from the GTK4 book should work with Python as well, there is no real reason to force all code in OOP classes. Maybe you can try to convert one of the basic example to Python, maybe this one: GTK4 for Graphical User Interfaces

1 Like

Ok, thank you both! For the concrete problem, this was the key part that I wasn’t getting right:

constructors don’t have positional arguments, only named ones.

I’m not only new to GTK4, it’s also my first time with GTK and, therefore, also with its python bindings, so probably this would have been obvious otherwise.

There isn’t GTK4-specific documentation, but you can read the GObject tutorial for Python, the GTK3 tutorial, and the GTK3 API reference.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.