Getting started with GTK app development

I find the current state of things very confusing for a new learner to start developing GTK apps.
There are no easy to understand tutorials, there are no online “Zero to hero” Udemy courses, there is very little tutorials on youtube and most of them are outdated. There also aren’t any books that don’t get very complicated on the 2nd page.

There is also a choice of deciding on a language, some recommend Python, some say C, others use Vala and we would all want to use Rust but there is no docs or tutorials at all. I’m not even sure if there are any complete GTK bindings for Rust.

Now we come to the issue of understanding an IDE such as Builder and Meson build system.

You can quickly see how this might be overwhelming for the new learners (like myself, being a web developer).

Although I wanted to start learning Rust GTK combo, I have settled for Vala to introduce myself to GTK as I found some great tutorials on youtube made by Alecaddd:
https://www.youtube.com/watch?v=7z0NVCrJr6A&list=PLriKzYyLb28mn2lS3c5yqMHgLREi7kR9-

It seems to be pretty Elementary OS centered but the issues are pretty “DuckDuckGo solvable”.

Let’s make this thread and ultimate “Getting started with GTK app development” thread where if you know a good resource for starting app development with GTK you can share it here, we could also help each other when we get stuck on something when the resources are outdated and I’ll try my best to edit and update this thread to have all the resources structured on the top.

[EDIT:] Nvm. There already seems to be a thread like this so visit: https://discourse.gnome.org/t/useful-documentation-for-gtk/29/3

10 Likes

Hi,
I started using the Gtk±2.0 tutorial
A great book is: Foundations of GTK+ Development; ISBN 978-1-4302-0386-5

1 Like

Here are some advices if you choose the C programming language.

Sooner or later, you as a newbie is going to recognize, you want to create objects for all kinds of problems. Just inherit from GObject all along the way. For visible elements inherit from GtkWidget or GtkContainer implementations and create your composite widgets.

Assumed you want to create a primitive type use GBoxed. I did so because glib lacks support for complex data type, take a look at my AgsComplex type.

You may want to organize your objects using singleton manager. Or provide a common entry point to your objects by creating an application context. It might be extensible by interfaces, just create your providers.

The goal of GObject is to provide OOP like programming to your C application. GObject allows you create properties. Make use of them by calling g_object_get() and g_object_set(). Learn to create your own properties.

Here I should mention glib-mkenums program, but didn’t use it yet.

Create events for your classes, use g_signal_new(). Create you own marshalers using glib-genmarshal program, which generates the code for you.

It took some time for me to realize, the very same class is assigned to multiple object instances of the very same type. This is why it is called class.

There is no interface heritage. Thought you can override the parent interface implementation.

And don’t forget to use gtk-doc.

2 Likes

My blog is about using GTK with the D language, but from what I understand, it’s similar to Rust. At least they’re both curly-brace languages.

Hope this helps.

I’m starting on my blog a series of articles on how to build a GTK application using Python, Builder, Meson and Flatpak. Here’s a link to the first article: https://belmoussaoui.com/2019/05/13/how-to-create-a-gtk-application-using-python-part-1/
More articles will be added to my blog and linked at the end of the first article once they’re public.

3 Likes

This sounds intriguing.

Just wondering…

Do you know where can I find instructions for setting up a pyGTK development environment on Windows? I found something for Linux, but it seems no one has written anything for other OSs.

So far, I’ve managed to install Python 3.7.3, but the pyGtk libraries and runtime? Nothing. I’m not even sure if they exist for Windows, to be honest.

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?