Getting Started — PyGObject

# Getting Started

To get things started we will try to run a very simple GTK based GUI application using the PyGObject provided Python bindings. First create a small Python script called `hello.py` with the following content and save it somewhere:

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

    window = Gtk.Window(title="Hello World")
    window.show()
    window.connect("destroy", Gtk.main_quit)
    Gtk.main()

Before we can run the example application we need to install PyGObject, GTK and their dependencies. Follow the instructions for your platform below.

Ubuntu | Fedora | Arch Linux
Windows | macOS | openSUSE

After running the example application have a look at the “Python GTK 3 Tutorial” for more examples on how to create GTK apps and the “PyGObject API Reference” for API documentation for all supported libraries. There are also typing stubs available here PyGObject-stubs.

## Windows

1. Go to http://www.msys2.org/ and download the x86_64 installer

2. Follow the instructions on the page for setting up the basic environment

3. Run `C:\msys64\mingw64.exe` - a terminal window should pop up

4. Execute `pacman -Suy`

5. Execute `pacman -S mingw-w64-x86_64-gtk3 mingw-w64-x86_64-python3 mingw-w64-x86_64-python3-gobject`

6. To test that GTK 3 is working you can run `gtk3-demo`

7. Copy the `hello.py` script you created to `C:\msys64\home\<username>`

8. In the mingw32 terminal execute `python3 hello.py` - a window should appear.

## Ubuntu / Debian

Installing the system provided PyGObject:

  1. Open a terminal

  2. Execute `sudo apt install python3-gi python3-gi-cairo gir1.2-gtk-3.0`

  3. Change the directory to where your `hello.py` script can be found (e.g. `cd Desktop`)

  4. Run `python3 hello.py`

Installing from PyPI with pip:

  1. Open a terminal and enter your virtual environment

  2. Execute `sudo apt install libgirepository1.0-dev gcc libcairo2-dev pkg-config python3-dev gir1.2-gtk-3.0` to install the build dependencies and GTK

  3. Execute `pip3 install pycairo` to build and install Pycairo

  4. Execute `pip3 install PyGObject` to build and install PyGObject

  5. Change the working directory to where your `hello.py` script can be found

  6. Run `python3 hello.py`

## Fedora

Installing the system provided PyGObject:

  1. Open a terminal

  2. Execute `sudo dnf install python3-gobject gtk3`

  3. Change the working directory to where your `hello.py` script can be found

  4. Run `python3 hello.py`

Installing from PyPI with pip:

  1. Open a terminal and enter your virtual environment

  2. Execute `sudo dnf install gcc gobject-introspection-devel cairo-gobject-devel pkg-config python3-devel gtk3` to install the build dependencies and GTK

  3. Execute `pip3 install pycairo` to build and install Pycairo

  4. Execute `pip3 install PyGObject` to build and install PyGObject

  5. Change the working directory to where your `hello.py` script can be found

  6. Run `python3 hello.py`

## Arch Linux

Installing the system provided PyGObject:

  1. Open a terminal

  2. Execute `sudo pacman -S python-gobject gtk3`

  3. Change the working directory to where your `hello.py` script can be found

  4. Run `python3 hello.py`

Installing from PyPI with pip:

  1. Open a terminal and enter your virtual environment

  2. Execute `sudo pacman -S python cairo pkgconf gobject-introspection gtk3` to install the build dependencies and GTK

  3. Execute `pip3 install pycairo` to build and install Pycairo

  4. Execute `pip3 install PyGObject` to build and install PyGObject

  5. Change the working directory to where your `hello.py` script can be found

  6. Run `python3 hello.py`

## openSUSE

Installing the system provided PyGObject:

  1. Open a terminal

  2. Execute `sudo zypper install python3-gobject python3-gobject-Gdk typelib-1_0-Gtk-3_0 libgtk-3-0`

  3. Change the directory to where your `hello.py` script can be found

  4. Run `python3 hello.py`

Installing from PyPI with pip:

  1. Open a terminal and enter your virtual environment

  2. Execute `sudo zypper install cairo-devel pkg-config python3-devel gcc gobject-introspection-devel` to install the build dependencies and GTK

  3. Execute `pip3 install pycairo` to build and install Pycairo

  4. Execute `pip3 install PyGObject` to build and install PyGObject

  5. Change the working directory to where your `hello.py` script can be found

  6. Run `python3 hello.py`

## macOS

1. Go to https://brew.sh/ and install homebrew

2. Open a terminal

3. Execute `brew install pygobject3 gtk+3`

4. Change the working directory to where your `hello.py` script can be found

5. Run `python3 hello.py`

For more details on how to use a virtualenv with PyGObject, see the “Creating a Development Environment” page.