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?
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.
# Load Gtk
from gi.repository import Gtk
# When the application is launched…
# … 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())
# Create a new application
app = Gtk.Application(application_id='com.example.GtkApplication')
# Run the application
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:
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: http://ssalewski.de/gtkprogramming.html#_application_style