DEV Community

loading...

Python3 + GTK+3 + Glade + Templates = <3

Cyber
Bio := struct {Identity, Pronouns, Undergrad, Also string}{ "🏳️‍⚧️ Trans woman", "👩🏻‍💻️ She/Her", "💾 Computer Engineer", "🔬 NLP Master Practitioner"}
・2 min read

Many software that you use on your Gnome environment could've been developed with Python. Apps like Gnome Music and Gnome Tweaks are some examples.

In this article I'll show you a minimal example in order to give you a foundation for your ideas. Let's go:

Create a basic layout on Glade:

GtkApplicationWindow

Stuff you need to pay attention to:

  • The top level component is a GtkApplicationWindow (you can find it under Toplevels button)
  • While it's selected, check the Composite checkbox on the farther right (super important!)
  • When you check the Composite checkbox the ID field becomes Class Name. Give it a name and save it, we'll use it soon
  • Give the button an ID and save it, we'll use it soon

With the Button selected, switch to the Signals tab:

Button Signals

  • Click on the Handler column on the clicked Signal row
  • Type the first letter of your button's name
  • Accept the suggestion (btnMain_clicked_cb, in my example) and save it, we'll use it soon

And that's it for Glade. Save your .glade file on your project dir.

Create a basic Python skeleton:

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

import sys


#
# 1. our .glade file (may contain paths)
#
@Gtk.Template.from_file("a.glade")
class AppWindow(Gtk.ApplicationWindow):
    #
    # 2. the GtkApplicationWindow class
    #
    __gtype_name__ = "app_window"

    #
    # 3. the Button id
    #
    btnMain: Gtk.Button = Gtk.Template.Child()

    #
    # 4. the signal handler name
    #
    @Gtk.Template.Callback()
    def btnMain_clicked_cb(self, widget, **_kwargs):
        assert self.btnMain == widget
        print(widget.get_label())


class Application(Gtk.Application):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, application_id="dev.monique.Gtk1",
                         flags=Gio.ApplicationFlags.FLAGS_NONE, **kwargs)
        self.window = None

    def do_activate(self):
        self.window = self.window or AppWindow(application=self)
        self.window.present()


if __name__ == '__main__':
    Application().run(sys.argv)
Enter fullscreen mode Exit fullscreen mode

You'll also need to:

pip install PyGObject
Enter fullscreen mode Exit fullscreen mode

It may complain about missing libraries, just read the error output and install the missing dependencies (I had to apt install libcairo2-dev libgirepository1.0-dev)

With this code you'll be able to get started.

Good luck!

Discussion (4)

Collapse
naruaika profile image
Naufan Rusyda Faikar

Woah, I just came to know that there is @Gtk.Template.Callback()! Thank you! I have been wondering this for a month or two!

Collapse
moniquelive profile image
Cyber Author

It’s really poorly documented. But there’s a pull request with some documentation waiting to be accepted...

Collapse
naruaika profile image
Naufan Rusyda Faikar

It's good to know that! I actually have read the source code, but couldn't realized what the Callback is for.

Collapse
texxg profile image
T.xx

I just made my very 1st python GTK app and I have to agree with you, the documentation is poor. And it’s such a great shame, as the tooling is otherwise so great! I love Glade!

I had to dig up answers to my guestions all over the planet, but eventually found everything I was looking for.

I have planned on making a blog post of some of the things that were difficult to solve (because of the documentation), so hopefully someone finds it helpfull. I never understood that one could contribute the documentation as well?

I also wish there would still be build windows binaries for the GTK, I never got Zeep working in MSYS :( But with windows was a snap. I am new with MSYS as well.