Getting started with GTK app development

I don’t think PyGTK exists for modern versions of python and gtk. What you need is PyGObject. This makes python work with any library that supports gobject introspection. Python g-i can be somewhat opaque, but gtk3 has good python-specific documentation. I would recommend you use PyCharm; it has special support for gi in its auto-completion/linting engine. If you don’t like PyCharm you can try fakegir for other editors.

TBH I find none of them work as well as auto-completion etc for most other languages. I would now choose gjs (with typescript rather than javascript for anything non-trivial, supported by ts for gjs) over python, but that has some issues of its own.

1 Like

Thanks for the info, Tony. I’ll look into these other Python options.

As to Typescript/Javascript, my interest is in cross-platform GTK and I write a blog/reference based on that. So far, I’ve been sticking to the D language. I did, at one point, look into Electron/Javascript/Typescript/etc. but decided to go with a one-language-one-toolkit approach.

But the other day I had a thought about writing a kind of Rosetta-esque doc for porting the examples I’ve written from D to C# to Python, and perhaps a few more. It’s a bad-burner sort-of thing ATM.

Anyway, thanks again. I’m sure this’ll help.

gjs isn’t related to Electron. It uses Mozilla’s javascript engine, SpiderMonkey, and adds gobject-introspection. A lot of the things you might expect with Electron, like the DOM, aren’t present, but you get native GTK instead. I’m pretty sure it works on Windows and OS X too. It can be considered to have quite a safe future, because large parts of gnome-shell use it.

Where I don’t need non-gobject libraries I’d rather use gjs than python. Since ES6 Javascript has become less quirky, and it has more advanced developer tools and editor/IDE support than python. The main disadvantage compared to python is that it doesn’t have convenient bindings to loads of non-gobject libraries. Its API is also somewhat unstable, because its old way of defining new GObjects is deprecated, but the decorator-based API its developers want to use isn’t really possible yet. Decorators still aren’t available in release versions of SpiderMonkey, but support functions could be written now to make the equivalent API available in Typescript.

Ah! Okay, I see what you’re saying.
Thanks.

The place to start looking for Python GObject bindings appears to be the Python GTK+ 3 Tutorial . The PyGObject API Reference is also a very useful resource, though it can take a lot of clicking around in the page to navigate between the search frame and the documentation frame.

I’ve been trying to write some general GTK/GNOME-related documentation for Python developers as part of the application development chapter of Purism’s Librem 5 developer documentation:
https://developer.puri.sm/Librem5/Apps/index.html

I was new to GTK/GNOME development when I started, so it’s been a learning experience for me as well.

(I can only put two links in this message, apparently.)

1 Like

Sorry for the long delay. I have already been able to build and execute a GTK application on Windows before for a university project using Python. I don’t remember how I did that and I don’t have any Windows machine to reproduce that.

No worries. Chances are, if it’s been a while, that everything’s changed anyway.

And for the record, I’m still looking for a solution.

The question @KristijanZic originally asked is a really good one and pretty much similar that I’ve been asking myself! It’s not clear what path a beginner should take. What language and tooling?

My 2 cents: choose a modern, widely used language and start leargning GTK. The concepts are the same, no matter the language.

My experience is from Python and D. Both are very neat languages and have good GTK bindings it seems. Python is of course much more widely used and offers better integrations to non-GTK stuff via various libraries.

I haven’t used gjs, but think it seems interesting! Maybe I’ll try it, when I get more grip with JavaScript overall.

I haven’t been considering Vala, because it’s not used outside Gnome/GTK community. That means less support to problems and also less integration with technology from outside.

D is also small language, but not so small as Vala, and offers interfacing with C++ and C libraries, if native D libraries can’t be found.

That all being said, I’d really like to see a short summary/landing page with “recommended technology” - meaning few recommended languages, links to best resources they have and also links to 2-3 editors/ides.

Now GNOME/newcomers site has links straight to different projects - most interesting of them coded in C. Not very newcomer-friendly… Python has an excellent GTK3 tutorial, but it’s not linked to GNOME Newcomers -page. Would it be possible to enrich that page a little bit?

And would https://gitlab.gnome.org/groups/GNOME/-/issues?label_name[]=8.+Developer+Docs be the correct place to offer help on that?

My 1 cent

a response to @heikkiket.

C is OK…

Now GNOME/newcomers site has links straight to different projects - most interesting of them coded in C. Not very newcomer-friendly…[…]

I would say that Gtk+ has a very good interface in C, and it is easy to get your head around what’s happening. (One of those things that was more magic in – say C++ – is quite straight forward in GObject/GLib code).

Sure, it is lots of code to write and it looks like a mess if you open up old projects, but modern GLib has some very nice macros that does a lot of the heavy lifting for you[1].

The only tedious thing I’ve encountered are doing enums “correctly” the GLib way[2]. The second one is creating signals[3] and properties[4].

The rest is very easy to understand and noob friendly now-a-days.

Also: Knowing the C interface is always good, independent of what language you are writing your Gtk+ app in.

Tooling:

Meson is your friend[5].

You can basically use any editor you want, but GNOME has it’s own editor GNOME Builder[6] you can have a look at, it’s quite good.

GNOME Builder has support for a wide range of languages too.

On Vala

Vala is not bad, it is basically like TypeScript or early C++ in that it is a language that compiles to an other language.

I like Vala personally, as it provides so much for you in the end. Even the documentation has come a long way in the last few years[7].

It can also be a good introduction to GLib based programming, as it introduces a lot of concepts and you can just look the the generated C code to figure out what it is doing.

(Personally I’ve seen more programs written in Vala than in Dlang, but that is probably just me).

Python

Use what ever resource you can find to get started if that is the way you want to go.


References

[1] GObject – 2.0

[2] GObject – 2.0

[3] gtk/gtkwidget.c · main · GNOME / gtk · GitLab + gtk/gtkwidget.c · main · GNOME / gtk · GitLab + gtk/gtkwidget.c · main · GNOME / gtk · GitLab (This needs a better tutorial… The one in the reference docs is lacklustre, to say the least.)

[4] GObject – 2.0

[5] Tutorial + GNOME module

[6] Apps/Builder - GNOME Wiki!

[7] Vala Reference Manual - Vala Reference Manual + https://valadoc.org/

[C++] glibmm: glibmm Reference Manual + gtkmm: gtkmm Reference Manual

[D] https://api.gtkd.org/ + GitHub - gtkd-developers/gir-to-d: Create D bindings from GObject introspection files

3 Likes

KDevelop and Builder, both are pretty good in code completion. Just sayin’.

Let state this: Vala is the most productive language for GLib/GObject/Gtk development. Is easy to learn and will help you to Learn GNOME technologies in short.

If we are pushing GNOME/Gtk consider to use Vala is a good choice, because the output C code is reusable to any other language. So you can create your business logic in Vala/GObject then use your favorite language for the target GUI for your target platform i.e. Android, if is GTK then you are at good place (is multi-platform too)

1 Like

Also Vala can bind really easy C API Libraries, so also is easy to see or use any other library linked against the C output of Vala code, with an Object Oriented Programing interface. Just look at valadoc.org

1 Like

I found this series through a YouTube search last week: https://prognotes.net/gtk-glade-c-programming/

The blog has screenshots of an older version of Glade but the videos he did in late 2019 are done with Glade 3.22.1 which is very helpful to me running Debian 10.

I’m getting my head around GtkBuilder using Glade and one thing that is immediately obvious is that there is so much less boilerplate that needs to be written with this approach. Plus, certain UI changes can be done in Glade that may not require rebuilding the program. Of course, GtkBuilder is not limited to C but since the program I am migrating from Gtk 2 to Gtk 3 is all handcoded Gtk C boilerplate, I think adopting the GtkBuilder approach will clean up the code considerably.

2 Likes

I tried PyCharm the last few days but couldn’t get auto-completion for Gtk objects working. Is there anything special I need to do/know?

I already searched the internet but couldn’t find a lot of information. I found the information to remove the limit on idea.max.intellisense.filesize in “pycharm/bin/idea.properties” but nothing changed. Another hint I found was, that the stubs need to be re-generated and that it could be done by pressing Alt+enter on the from gi.repository import Gtk line and choosing “generate stubs” from the menu. But the “generate stubs” options doesn’t exists here on the Pycharm 2020.1.2 community edition. :frowning_face:

So I’m a little bit clueless at the moment… Any hints?

Sorry, I haven’t used PyCharm for a long time. You’ll probably have to ask for help in Jetbrains’ support forums. Have you configured your project to use /usr/bin/python3 and installed python-gobject/PyGObject?

I vaguely remember using “generate stubs”, so if it’s gone I hope that doesn’t mean PyCharm no longer supports GI. If that is the case, try fakegir instead; VS Code is arguably the best alternative to PyCharm. It could also be worth checking whether gnome-builder has an equivalent feature.

Thanks for you reply! :slightly_smiling_face:

Yes, I use the default interpreter. python-gobject is installed as well. I can execute Python GTK programs just fine.

I also tried VS Code, there I couldn’t get auto-completion for GTK objects working as well :frowning:
Do you use VS Code at the moment, or what is your preferred IDE? Maybe you can give some tips how to get it working with VS Code?

Builder was what I tried first, which also doesn’t has reliable auto-completion for GTK objects: No auto-completion for python project in Gnome Builder

I mostly use VS Code or vim now, but I haven’t been writing anything using PyGObject lately. Did you install fakegir and follow its instructions to enable it in VS Code?

Yes, unfortunately it also didn’t worked.

$ ls .cache/fakegir/gi/repository/
cairo.py     DBus.py        freetype2.py  GCi.py  GIRepository.py  GL.py       GObject.py   libxml2.py  win32.py   xft.py   xrandr.py
DBusGLib.py  fontconfig.py  GCalc.py      Gio.py  GLib.py          GModule.py  __init__.py  Vulkan.py   xfixes.py  xlib.py

settings.json:

"python.autoComplete.extraPaths": [
    "/home/schiesbn/.cache/fakegir/"
],

Surprisingly, when I start the interactive python interpreter directly in the terminal I can auto-complete GTK objects and their methods. So it seems like technically everything needed is installed on the system. Also the python3-gi package is installed. But for whatever reasons neither VS Code nor Pycharm uses it.

I forgot something else, are you using type hints? They might be necessary for the auto-completion to work, otherwise there aren’t enough clues for Jedi or whatever to work out the type of the object you’re editing.

I think that is sharp but how do i use c++, or is it that there is no support for it?