Python is an accessible language that is perfect for data analysis and web development. But it’s a great choice for GUI app development too. GTK+ and Glade simplify the process of creating simple cross-platform apps.

GTK+ and Glade Interface Designer for Python Developers

GTK+ (GIMP Toolkit) and Glade Interface Designer are a terrific combination for Python developers who want to create intuitive and pleasing graphical user interfaces.

GTK+ is a multiplatform toolkit that you can use for GUI development. It is compatible with a variety of operating systems, including Linux, Windows, and macOS.

Glade Interface Designer, a companion tool to GTK+, lets you design GUIs without having to write layout code. You can use it to create a GUI in a WYSIWYG environment with a few clicks and simple drag-and-drop functions.

Setting Up Your Python Environment for GTK+ and Glade Development

Setting up your environment is a critical first step to ensure a smooth and efficient workflow.

1. Install Python

Start by making sure you have Python installed on your system. You will see Python 3 code in the examples you read, as it offers better support and integration for GTK+ and Glade. For Linux and macOS, Python is usually preinstalled.

Windows users can download Python from the official Python website.

2. Install GTK+

You can install GTK+ using a package manager.

For Ubuntu and Debian-based Linux systems use:

 sudo apt-get install libgtk-3-dev 

For Fedora and similar:

 sudo dnf install gtk3-devel 

On macOS, using Homebrew:

 brew install gtk+3 

Windows users can download GTK+ from GTK's official download page. But if you have MSYS2 installed, you can open the MSYS2 command line and use this command:

 pacman -S mingw-w64-x86_64-python-gobject 

3. Install Glade

You can use the command line to install Glade.

For Ubuntu and Debian-based Linux distributions:

 sudo apt-get install glade 

On Fedora:

 sudo dnf install glade 

macOS users can use Homebrew:

 brew install glade 

Windows users can use the following command with MSYS2:

 pacman -S mingw-w64-x86_64-glade 

4. Python Bindings for GTK+

Install PyGObject to integrate GTK+ with Python. The command you will use for this is:

 pip install PyGObject 

If there is an error like "Building wheel for pycairo (pyproject.toml) did not run" during the PyGObject installation, you will need to install the cairo package as well.

For Ubuntu and Debian-based Linux distributions:

 sudo apt-get install libcairo2-dev 

For Fedora:

 sudo yum install cairo-devel 

On macOS:

 brew install pygobject3 

5. Setting Up a Virtual Environment (optional)

It’s good practice to use a virtual environment for your Python projects. This isolates your project dependencies. Create and activate a virtual environment on Linux with these terminal commands:

 python -m venv myenv
source myenv/bin/activate

On Windows use:

 python -m venv myenv
myenv\Scripts\activate

On macOS, to ensure that the virtual environment can access the packages that brew installs, use:

 python -m venv --system-site-packages myenv
source myenv/bin/activate

6. Verifying the Installation

To verify that GTK+ and Glade are installed, create a simple Python script that imports GTK:

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

print("GTK+ version:", Gtk.get_major_version(), Gtk.get_minor_version())

When you run this script, it will output the installed GTK+ version. If everything goes well, you have set up your development environment.

Creating a Simple GUI App With Glade Interface Designer and Python

You can design your GUI app in Glade Interface Designer and export the layout as a project file. You can then access that project file from your Python code.

Designing Your GUI With Glade

Glade's drag-and-drop interface makes it easy to focus on the design without getting bogged down in the underlying code. Start Glade from your system's application menu or command line with this command:

 glade

You should see the Glade interface where you can start creating your GUI layout.

The Create New Project button in the top left provides a blank canvas for your GUI design. Glade offers a wide variety of widgets in its top bar, including buttons, text inputs, and labels. Drag these widgets onto your canvas to start styling your GUI. You can resize and position widgets according to your design needs.

First, select the GtkWindow widget from the Toplevels menu:

GtkWindow Creation Option in Glade
Screenshot by Writer - Fatih Küçükkarakurt

On the General page in the right bar of Glade, you will see an ID option. This ID is the unique name of the widget you added. For this example, assign the ID myMainWindow to the GtkWindow you added.

You can now add widgets to the main window you’ve created. Go to Containers in the top bar, select the GtkBox widget, and drag it to your workspace. Then give it an ID, myMainBox.

Creating ID for Glade GtkBox widget
Screenshot by Writer - Fatih Küçükkarakurt

After adding the GtkBox widget, you'll see various options to the right of your workspace that are specific to that widget. You can edit your entire design here without writing any code.

Next, add a Control widget to your design. To do so, go to Control in the top bar, select GtkButton as an example, and drag it anywhere in the GtkBox. Give it ID, myButton. If you want, you can also change the text of the button using the General tab in the right-hand panel.

GtkButton is a clickable widget, so you can define a Python handler for it and write the appropriate code later. Go to the Signals tab in the right menu and specify a handler for the clicked signal. For this example, call it on_button_clicked.

Setting Click Signal for Glade GtkButton
Screenshot by Writer - Fatih Küçükkarakurt

You can now save your GUI design as a project file. Save the file as myDesign.glade.

Using the Glade Design File From Python Code

Create an app.py file in the same directory as your myDesign.glade file. Paste the following code into this file:

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

class MyApp:
    def __init__(self):

        # Load the Glade file to construct the GUI

        self.builder = Gtk.Builder()
        self.builder.add_from_file("myDesign.glade")


        # Retrieve the main window from Glade and set up the close event

        self.window = self.builder.get_object("myMainWindow")
        self.window.connect("destroy", Gtk.main_quit)


        # Retrieve the button from Glade and connect the click event

        self.button = self.builder.get_object("myButton")
        self.button.connect("clicked", self.on_button_clicked)


        # Variable to toggle the background color

        self.color_toggle = False

    def on_button_clicked(self, widget):

        # Click the button and the background color will change

        color = "#FF0000" if self.color_toggle else "#00FF00"
        self.window.modify_bg(Gtk.StateType.NORMAL, Gdk.color_parse(color))
        self.color_toggle = not self.color_toggle

    def run(self):
        self.window.show_all()
        Gtk.main()

if __name__ == "__main__":
    app = MyApp()
    app.run()

This code will change the color of the background every time you click on the button. Note the calls to self.builder.get_object() which pass the IDs of the widgets that you defined in Glade.

Run your Python script using this command to see the result:

 python3 app.py 
Result for Glade and Python program
Screenshot by Writer - Fatih Küçükkarakurt

Advantages of Using GTK+ and Glade for Python GUI Development

Using GTK+ and Glade for Python GUI development offers clear benefits. Usually, creating a GUI takes a lot of time and effort. But with Glade Interface Designer, you can speed up the process. Glade also offers a wide range of widgets, each as easy to use as the button in the example.

A key advantage of Glade is its ability to keep GUI design separate from the code. This makes maintaining and updating the GUI simpler. This approach leads to cleaner, better-organized code, in line with modern programming practices.