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 | openSUSE |
Windows | macOS | PyPI |
Windows¶
- Go to http://www.msys2.org/ and download the x86_64 installer
- Follow the instructions on the page for setting up the basic environment
- Run
C:\msys64\mingw32.exe
- a terminal window should pop up - Execute
pacman -Suy
- Execute
pacman -S mingw-w64-i686-gtk3 mingw-w64-i686-python2-gobject mingw-w64-i686-python3-gobject
- To test that GTK+3 is working you can run
gtk3-demo
- Copy the
hello.py
script you created toC:\msys64\home\<username>
- In the mingw32 terminal execute
python3 hello.py
- a window should appear.
Ubuntu / Debian¶
- Open a terminal
- Execute
sudo apt install python-gi python-gi-cairo python3-gi python3-gi-cairo gir1.2-gtk-3.0
- Change the directory to where your
hello.py
script can be found (e.g.cd Desktop
) - Run
python3 hello.py
Fedora¶
- Open a terminal
- Execute
sudo dnf install pygobject3 python3-gobject gtk3
- Change the directory to where your
hello.py
script can be found (e.g.cd Desktop
) - Run
python3 hello.py
Arch Linux¶
- Open a terminal
- Execute
sudo pacman -S python-gobject python2-gobject gtk3
- Change the directory to where your
hello.py
script can be found (e.g.cd Desktop
) - Run
python3 hello.py
openSUSE¶
- Open a terminal
- Execute
sudo zypper install python-gobject python3-gobject gtk3
- Change the directory to where your
hello.py
script can be found (e.g.cd Desktop
) - Run
python3 hello.py
macOS¶
- Go to https://brew.sh/ and install homebrew
- Open a terminal
- Execute
brew install pygobject3 --with-python@2 gtk+3
to install for both python2 and python3 - Change the directory to where your
hello.py
script can be found (e.g.cd Desktop
) - Run
python3 hello.py
From PyPI¶
PyGObject is also available on PyPI: https://pypi.org/project/PyGObject
For this approach you have to make sure that all runtime and build dependencies are present yourself as pip will only take care of pycairo.
virtualenv --python=python3 myvenv
source myvenv/bin/activate
pip install pygobject
python hello.py
For more details on how to use a virtualenv with PyGObject, see the “Creating a Development Environment” page.