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:
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:
- 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 name we saved above
#
btnMain: Gtk.Button = Gtk.Template.Child()
#
# 4. the signal handler name we saved above
#
@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)
You'll also need to:
pip install PyGObject
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!
Top comments (6)
Thank you! It's 2024 and I haven't found that anywhere else. Maybe you could bold the "(super important!)" in the "Composite checkbox" item. I actually didn't do it and only read it the second time through.
To use this with the pyinstaller, I had to determine the glade path at runtime:
Thank you for this post, very helpful and exactly what I was looking for.
Woah, I just came to know that there is
@Gtk.Template.Callback()
! Thank you! I have been wondering this for a month or two!Itβs really poorly documented. But thereβs a pull request with some documentation waiting to be accepted...
It's good to know that! I actually have read the source code, but couldn't realized what the
Callback
is for.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.