Friday, December 13, 2013

Install and program with GTK+ 3.0 in Python

To program Python with GTK+ 3.0, libgtk-3-dev have to be installed:
$ sudo apt-get install libgtk-3-dev
(referene: Install GTK+ 3.0 on Raspberry Pi)

Once installed, Python 2 (python and IDLE) can work use GTK+ 3.0 library to programming with GUI.

Dummy example of Python program with GTK+ 3.0
#!/usr/bin/python
from gi.repository import Gtk

win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

"Hello World" of Python 2.7.3 with GTK+ 3
"Hello World" of Python 2.7.3 with GTK+ 3
To work with Python 3, python3-gi is needed. Otherwise will have ImportError of "No module named gi.repository". To install python3-gi, enter command:
sudo apt-get install python3-gi

ImportError: No module named gi.repository
ImportError: No module named gi.repository

"Hello World" of Python 3.2.3 with GTK+ 3
"Hello World" of Python 3.2.3 with GTK+ 3

The Python GTK+ 3 Tutorial gives an introduction to writing GTK+ 3 applications in Python.

No comments: