keepcalculator

06 Numbers and Powers

Python GUI Builder with Glade & GTK: How It Works

Card No. 06·66821 · Updated · Standard formula, shown below

Yes, you can build a Python GUI with Glade and GTK: Glade is a visual interface designer that saves your layout as an XML file, and your Python code loads that file using PyGObject (the GTK 3 Python bindings). You get a drag-and-drop editor for windows, buttons, and layouts, while your Python script handles the logic and connects to the widgets by their assigned names. This workflow separates the visual design from the code, making it faster to prototype and easier to maintain the interface separately from the application logic.

Background: What Are Glade and GTK?

GTK (GIMP Toolkit) is a cross-platform toolkit for creating graphical user interfaces. It started as the toolkit for the GIMP image editor and has grown into the foundation for many Linux desktop applications, including GNOME’s default apps. Glade is a standalone RAD (Rapid Application Development) tool that lets you design a GTK interface visually. Instead of writing widget-creation code line by line, you drag buttons, text entries, and containers onto a canvas, set their properties (like labels, sizes, and signals), and save the result as a .glade file (which is actually an XML file).

For Python, the binding is called PyGObject (formerly PyGTK for GTK 2). It provides a complete, object-oriented API to GTK 3. While GTK 4 is now the current major version, Glade’s official support is for GTK 3. Many production applications still use GTK 3, and the tools are stable and well-documented.

How the Glade + Python Workflow Works

The process is straightforward:

  1. Design the UI in Glade: Launch Glade, create a new GTK 3 project, and drag widgets (e.g., GtkWindow, GtkButton, GtkLabel) onto the canvas.
  2. Set widget properties: Give each widget a unique ID (like main_button) and connect signals (like clicked) to handler names (like on_button_clicked).
  3. Save the file: Save as my_ui.glade (or my_ui.ui).
  4. Load it in Python: Use Gtk.Builder to load the XML file, then call builder.get_object("widget_id") to get references to widgets and builder.connect_signals(handler_object) to connect the signal handlers defined in your Python class.
  5. Run the main loop: Start the GTK main loop with Gtk.main().

Here’s a minimal example:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk

class Handler:
    def on_button_clicked(self, button):
        print("Button clicked!")

builder = Gtk.Builder()
builder.add_from_file("my_ui.glade")
builder.connect_signals(Handler())

window = builder.get_object("main_window")
window.connect("destroy", Gtk.main_quit)
window.show_all()
Gtk.main()

This pattern is the core: you never create widgets in Python; you just fetch them by their Glade-assigned IDs.

Choosing Between Glade and Hand-Coding

When should you use Glade? It’s ideal if you are comfortable with a visual editor and want to iterate quickly on the layout. It’s also helpful for designers who don’t write code but need to mock up a UI. However, hand-coding with PyGObject is often better when you need dynamic interfaces—for example, a list of items that changes based on user input, or complex custom widgets. Glade is best for static layouts; dynamic parts you still have to build in code.

Another consideration: Glade files are XML, and they can become messy if you have many nested containers. Some developers find it harder to review changes in a Git diff compared to code. But for a small to medium app, Glade can save hours of trial-and-error with packing and alignment.

Common Problems and How to Solve Them

Here are the typical pitfalls when using Glade with Python:

  • GTK version mismatch: Glade saves GTK 3 files, but your Python environment might have GTK 2 or GTK 4 installed. Ensure you have PyGObject for GTK 3 (e.g., python3-gi on Debian/Ubuntu, pygobject on Fedora, or install via pip with pip install PyGObject).
  • Signal handler not found: If you get an error like TypeError: object of type 'GtkButton' has no attribute 'on_button_clicked', it means the handler method is not in the class you passed to connect_signals. Double-check the exact method name and that the class is instantiated.
  • Widget not found: builder.get_object() returns None if the ID is misspelled. Check the Glade file for the exact ID (case-sensitive).
  • Window doesn’t show: Forgetting to call window.show_all() is a common mistake. Also, if you set visible to False in Glade, you must show it manually.
  • Deprecation warnings: Some GTK 3 widgets are deprecated (like Gtk.HBox). Use the newer Gtk.Box with horizontal orientation.

Alternatives to Glade

Glade is not the only visual builder for GTK. Here are the main alternatives:

  • Cambalache: The official replacement for Glade, under active development for GTK 4. It has a similar XML-based workflow but supports the newer toolkit.
  • Hand-coding with PyGObject: Writing the UI directly in Python gives you full control and is often easier for dynamic interfaces.
  • Other toolkits: If you are not tied to GTK, consider Qt for Python (PySide6) with its Qt Designer, or Tkinter (built-in but less modern). For web-based UIs, Flask or Django with HTML/CSS/JS are popular.

Each has its own trade-offs in terms of licensing, look-and-feel, and learning curve.

Costs and Licensing

Both Glade and GTK are free and open-source. GTK is licensed under the GNU LGPL, which allows you to use it in proprietary applications as long as you dynamically link to it and allow users to replace the library. Glade itself is licensed under the GPL, but the files it produces (the XML) are not considered derivative works—you can use them in any project without licensing your code under the GPL. In practice, you can build commercial applications with Glade and GTK without paying any licensing fees.

Safety and Best Practices

There is no safety risk in using Glade itself, but there are best practices for maintainable code:

  • Keep the Glade file and Python code in sync by using a version control system.
  • Use descriptive widget IDs (e.g., submit_button instead of button1).
  • Separate UI logic from business logic—put signal handlers in a separate class or module.
  • Handle the destroy signal to properly quit the main loop.
  • For packaging, include the .glade file as a data file in your distribution.

Frequently Asked Questions

Is Glade still maintained?

Glade is in maintenance mode; the last stable release was 3.22.2 in 2021. It still works fine for GTK 3, but new development has moved to Cambalache for GTK 4. If you are starting a new project, consider whether you want to use the older toolkit for stability or the newer one for future-proofing.

Can I use Glade with GTK 4?

No, Glade does not support GTK 4. Use Cambalache instead, or hand-code with PyGObject using GTK 4. The XML format is different, and many widgets have changed.

How do I install Glade and PyGObject?

On Debian/Ubuntu, run sudo apt install glade python3-gi python3-gi-cairo gir1.2-gtk-3.0. On Windows, you can install PyGObject from MSYS2 or use the pre-built wheels from the PyGObject project. On macOS, use Homebrew: brew install glade pygobject3.

Can I edit the .glade file manually?

Yes, it’s XML, so you can edit it in any text editor. However, it’s easy to break the structure. It’s safer to make changes in Glade, but simple tweaks like changing a label or size can be done by hand.

What is the difference between .glade and .ui files?

They are the same format; Glade saves with a .glade extension, but the content is identical to a .ui file. You can rename it to .ui if you prefer, and Gtk.Builder will load it the same way.

Can I use Glade for commercial projects?

Yes. The XML files are not encumbered by the GPL, and GTK’s LGPL allows proprietary use. You just need to comply with the LGPL by allowing users to swap the GTK library.

Related

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.

More cards in this drawer

The whole Numbers and Powers drawer →