I was confused by pygtk
vs gtk
because my test app worked fine with gtk
.
I now realise that gtk
means PyGTK, the Python module that contains bindings to the Gtk+ 2.x library. pygtk
is another module, also part of PyGTK, that can (but doesn't need to) be used to ensure a certain version of the Gtk+ library.
The gtk
and pytk
modules apply to Python 2 only. For Python 3 you need to use Gtk+ through the gi
GObject Introspection module.
The Python GObject Introspection module (aka gi
or gi.repository
) is a middleware layer between C libraries (using GObject) and language bindings. Essentially it autogenerates Python bindings from the underlying C source code. GObject is the GLib Object system, where GLib provides the core application building blocks for libraries and applications written in C. GObject provides GTK+ with object-oriented C-based APIs and automatic transparent API bindings to other compiled or interpreted languages (such as Python).
The GTK3+ library uses Gobject but the GTK2+ library does not. This means that you can not use Gtk2+ with Python version 3.
For Python 2 you'd use
import pygtk
pygtk.require('2.0')
import gtk
or just
import gtk
For Python 3 you need
import gi
gi.require_version('Gtk','3.0')
from gi.repository import Gtk
You can verify versions with:
For Gtk+ 2 (Python 2 or 3)
print("PyGTK %d.%d.%d Gtk+ %d.%d.%d" % (gtk.pygtk_version + gtk.gtk_version))
For Gtk+ 3 (Python 3 only)
print("Gtk %d.%d.%d" % (Gtk.get_major_version(),
Gtk.get_minor_version(),
Gtk.get_micro_version()))