Gtk4 documentation for Python

I have a Python application with the GUI provided by Glade and Gtk3. Gtk4 has now been released and Glade won’t be supporting it, so I was aiming to be ahead of the game by switching to Gtk4 now. The first thing I did was look for examples and documentation. This example GitHub - Taiko2k/GTK4PythonTutorial: GTK4 + Python tutorial with code examples is excellent, from that I got most of what I needed working very quickly, in fact it was simpler and quicker than using Glade! However I need a few other things and I can’t find any useful examples or documentation. I’ve tried https://docs.gtk.org, but what is available there is either too theoretical or designed for C. Since then I have wasted too much time browsing for information and guessing what the syntax might be and failing. At the moment it looks as though my best plan would be to stick with Gtk3 until it’s impossible to use it any longer, then decide which tool to replace it with. I’d prefer to use Gtk4, but that would only be possible if someone could point me to some comprehensive examples or appropriate documentation.

1 Like

It would probably be helpful if you explained what kind of documentation and/or examples you’re looking for.

For examples GitHub - Taiko2k/GTK4PythonTutorial: GTK4 + Python tutorial with code examples is excellent, however I need a few other things. For documentation, something like this: https://athenajc.gitbooks.io/python-gtk-3-api/content/gtk-group/gtkdialog.html

If you’re looking for the GTK4 API reference for Python, then you want:

Of course, you have to remember two things—which also apply to the GTK3 API reference available in its original place:

  1. the pygobject API reference is generated from the same source as the C API reference, with some massaging of the content, but it won’t change the embedded examples
  2. the pygobject API reference may not match the latest version of GTK available, because it’s generated on a separate container and does not use the same introspection data provided by the GNOME SDK

In general, it would be great if somebody contributed some time to get the pygobject API references built against the introspection data of the GNOME SDK, instead of using a base Linux distro. Additionally, it would be great if more people contributed documentation for Python devs. The tutorials on the GNOME Developers documentation website have examples available in Python, for instance.

That’s why I asked what kind of documentation you’re looking for; if it’s tutorials that you are missing, then specifying what kind of tutorials you need would help people writing the docs. Staying vague isn’t going to make content magically appear.

1 Like

I stated the type of examples I was looking for in my OP. I gave an example of the type of documentation I was looking for in my second in answer to your request. I’m not clear why you think I’m “staying vague” and I obviously don’t expect “to make content magically appear.” It may be a question of culture but to me as a Brit that was somewhat impolite…

I’m sorry, but no: it wasn’t clear at all. And being a Brit has nothing to do with it.

The biggest problem I have right now is that there is a shortage of
python-gtk4 examples. I am not sure why. There are many python-gtk3
examples, in particular at laska’s or lazka’s site. There is not really
any content for python-gtk4 there. I think the simplest explanation is
that the python devs have not yet written a whole lot of python-gtk4,
or it is hosted elsewhere. So this may change in the coming weeks and
months.

Normally I found laska’s website the best; I can’t find it right now, but it had explanations as well as working code there. It was really good.

In part because GTK4 is, compared to GTK3, fairly new: it was released nearly two years ago but it has been included in the latest Ubuntu LTS release, for instance.

On top of that, the pygobject bindings haven’t been heavily maintained; Christoph Reiter (lazka) is the only maintainer, and not many people contribute to the project. The lower level of maintenance is coupled with GTK4 introducing a bunch of API that takes advantage of features in the type system that have never been implemented in pygobject over 20 years.

Having said that, it’s not like GTK4 is a completely different toolkit from GTK3; a lot of functionality is precisely the same, so if you know GTK3 you’re already more than halfway there with GTK4. Additionally, the C API reference has been reworked to try and be familiar to people using it from other, non-C languages; and the GNOME developer documentation has been improved to include more tutorials, with examples in multiple programming languages.

To sum up:

  • the GNOME developers documentation tries to be language agnostic, or at least to cover more than just C in its examples; more tutorials and examples are welcome
  • pygobject maintenance is currently “best effort”; more people contributing fixes, features, and documentation are very much welcome, otherwise things will not improve on their own
  • there are holes in the pygobject support that have been exposed by GTK4; these need an implementation, otherwise examples cannot be written

As I said above, the important thing at this point is being more explicit when asking about documentation; you can use Discourse to ask for examples of specific functionality, or you can file an issue explaining what you’re looking for; you can also write a tutorial and contribute it. For pygobject-specific documentation, you can also contribute it to the pygobject project, which contains the source of the pygobject website.

1 Like

For GTK 4, you can use Cambalache:
https://blogs.gnome.org/xjuan/

Its latest tag release is 0.10.3. But it is already usable.

Cambalache is a new RAD tool for Gtk 4 and 3 with a clear MVC design and data model first philosophy.

https://gitlab.gnome.org/jpu/cambalache

“Normally I found laska’s website the best”/“lazka”. I can’t find any references to this, could you help me with a link please?

“For GTK 4, you can use Cambalache”. That’s a great tip. I had already discovered Cambalache and was impressed that it looked better designed than Glade. I decided to try writing Gtk4 too and then decide between the two approaches. I found using Gtk4 directly was quite quick and easy and would eliminate an intermediate step that could cause problems later if Cambalache wasn’t well supported, so decided to go with that. However, it looks as though I might have to change my mind and go back to Cambalache, or switch to another tool.

The “original place” link in reply Gtk4 documentation for Python - #4 by ebassi is to Lazka’s website.

“Lazka’s website” has a link for Gtk3 with examples in C.

IMHO, that’s fine for small GUI. And that is what I do for my personal needs. But for a bigger GUI, probably a RAD tool like Cambalache can improve your productivity as you just need to design your GUI with that tool, and code the needed callback functions.

Another point is that you can easily test the numerous options and properties of each widget in Cambalache: you just have to save the .ui file and relaunch your application to see the result. No coding! But there is another way to do that, and on the fly, with GtkInspector!

“…fine for small GUI…But for a bigger GUI, probably…Cambalache…”
That’s interesting, because when I was using Glade I got the impression from somewhere that it was only for “amateurs” and “real programmers” coded directly in GTK3. You seem to imply that serious applications are mostly created using a tool like Cambalache? It seems unlikely as it hasn’t been around very long.
Qt can be done like that of course, my app originally used PyQt but I decided to make it more Gnome-like and use Gtk3.

You may be right. I was just expressing a personal opinion. I don’t know what professional programmers do…

I was not talking specifically about Cambalache, but RAD tools like Cambalache and Glade in general. Of course Cambalache is yet in early development (although the main author said 6 days ago Cambalache was “close to be feature parity with Glade” in this thread).

Note: on Patreon he says:

Hi my name is Juan Pablo I been one of Glade developers since 2005 and maintainer since 2012. I am developing a new RAD tool called Cambalache that enables the creation of user interfaces for Gtk 4 and the GNOME desktop environment.

More examples https://github.com/yucefsourani/python-gtk4-examples

Having wasted hours searching for useful documentation or examples I’ve resorted to guesswork. I’ve managed a bit of a dialogue box so far -without getting the buttons to do anything though.

def	showWarning(self,	buttonTest):
  self.message = Gtk.MessageDialog(title = 'Warning', text = 'Something has gone wrong')
  self.message.add_buttons('OK', 1)	
  self.message.set_transient_for(self)	# Makes the dialog always appear in front of the parent window
  self.message.set_modal(self)	        # Makes the parent	window unresponsive while dialog is showing
  self.message.show()

So, at this point the question is: what are you trying to achieve?

Creating a message box is kind of pointless—this is not HTML and JavaScript, there’s no point in writing alert("Hello").

This is good, but not for GTK4 PyGTK - MessageDialog Class