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:
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.
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)
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.