GTK and PYGTK difference
many programmers import both gtk and pygtk in this way:
import gtk
import pygtk
I have created a simple program using only gtk and it works:
import gtk
window = gtk.Window()
window.set_size_request(800, 700)
window.set_position(gtk.WIN_POS_CENTER)
window.connect("destroy", gtk.main_quit)
button = gtk.B开发者_运维问答utton("Vai")
button.set_size_request(30, 35)
button.connect("clicked", naviga)
testo = gtk.Entry()
h = gtk.HBox()
h.pack_start(testo)
h.pack_start(button)
window.add(h)
window.show_all()
gtk.main()
So... the question is: what is the difference from GTK and PYGTK ?
pygtk is provided by python-gobject.
gtk is provided by python-gtk2.
pygtk provides the pygtk.require function which allows you to require that a certain version of gtk (or better) is installed. For example
import pygtk
pygtk.require('2.0')
importing gtk only is possible, but your program may not work as expected on someone else's machine if their version of gtk is older.
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()))
加载中,请稍侯......
精彩评论