blob: f697cd3c51242aed89298dc8cae5c364edd83fe9 [file] [log] [blame]
Georg Brandld7413152009-10-11 21:25:26 +00001:tocdepth: 2
2
3==========================
4Graphic User Interface FAQ
5==========================
6
7.. contents::
8
Georg Brandl62423cb2009-12-19 17:59:59 +00009.. XXX need review for Python 3.
10
11
Georg Brandld7413152009-10-11 21:25:26 +000012General GUI Questions
13=====================
14
15What platform-independent GUI toolkits exist for Python?
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000016========================================================
Georg Brandld7413152009-10-11 21:25:26 +000017
Antoine Pitroueb50ba52011-02-05 10:43:57 +000018Depending on what platform(s) you are aiming at, there are several. Some
19of them haven't been ported to Python 3 yet. At least `Tkinter`_ and `Qt`_
20are known to be Python 3-compatible.
Georg Brandld7413152009-10-11 21:25:26 +000021
22.. XXX check links
23
24Tkinter
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000025-------
Georg Brandld7413152009-10-11 21:25:26 +000026
27Standard builds of Python include an object-oriented interface to the Tcl/Tk
Antoine Pitroueb50ba52011-02-05 10:43:57 +000028widget set, called :ref:`tkinter <Tkinter>`. This is probably the easiest to
29install (since it comes included with most
30`binary distributions <http://www.python.org/download/>`_ of Python) and use.
31For more info about Tk, including pointers to the source, see the
32`Tcl/Tk home page <http://www.tcl.tk>`_. Tcl/Tk is fully portable to the
33MacOS, Windows, and Unix platforms.
Georg Brandld7413152009-10-11 21:25:26 +000034
Benjamin Petersonf6489f92009-11-25 17:46:26 +000035wxWidgets
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000036---------
Georg Brandld7413152009-10-11 21:25:26 +000037
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000038wxWidgets (http://www.wxwidgets.org) is a free, portable GUI class
39library written in C++ that provides a native look and feel on a
40number of platforms, with Windows, MacOS X, GTK, X11, all listed as
41current stable targets. Language bindings are available for a number
42of languages including Python, Perl, Ruby, etc.
Georg Brandld7413152009-10-11 21:25:26 +000043
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000044wxPython (http://www.wxpython.org) is the Python binding for
45wxwidgets. While it often lags slightly behind the official wxWidgets
46releases, it also offers a number of features via pure Python
47extensions that are not available in other language bindings. There
48is an active wxPython user and developer community.
Benjamin Petersonf6489f92009-11-25 17:46:26 +000049
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000050Both wxWidgets and wxPython are free, open source, software with
51permissive licences that allow their use in commercial products as
52well as in freeware or shareware.
53
Georg Brandld7413152009-10-11 21:25:26 +000054
55Qt
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000056---
Georg Brandld7413152009-10-11 21:25:26 +000057
Antoine Pitroueb50ba52011-02-05 10:43:57 +000058There are bindings available for the Qt toolkit (using either `PyQt
59<http://www.riverbankcomputing.co.uk/software/pyqt/>`_ or `PySide
60<http://www.pyside.org/>`_) and for KDE (`PyKDE <http://www.riverbankcomputing.co.uk/software/pykde/intro>`__).
61PyQt is currently more mature than PySide, but you must buy a PyQt license from
62`Riverbank Computing <http://www.riverbankcomputing.co.uk/software/pyqt/license>`_
63if you want to write proprietary applications. PySide is free for all applications.
64
65Qt 4.5 upwards is licensed under the LGPL license; also, commercial licenses
66are available from `Nokia <http://qt.nokia.com/>`_.
Georg Brandld7413152009-10-11 21:25:26 +000067
68Gtk+
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000069----
Georg Brandld7413152009-10-11 21:25:26 +000070
Antoine Pitrou7462e872011-12-03 20:59:24 +010071The `GObject introspection bindings <https://live.gnome.org/PyGObject>`_
72for Python allow you to write GTK+ 3 applications. There is also a
73`Python GTK+ 3 Tutorial <http://python-gtk-3-tutorial.readthedocs.org/en/latest/>`_.
74
75The older PyGtk bindings for the `Gtk+ 2 toolkit <http://www.gtk.org>`_ have
76been implemented by James Henstridge; see <http://www.pygtk.org>.
Georg Brandld7413152009-10-11 21:25:26 +000077
78FLTK
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000079----
Georg Brandld7413152009-10-11 21:25:26 +000080
81Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
82powerful and mature cross-platform windowing system, are available from `the
83PyFLTK project <http://pyfltk.sourceforge.net>`_.
84
85
86FOX
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000087----
Georg Brandld7413152009-10-11 21:25:26 +000088
89A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
90<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
91and Windows.
92
93
94OpenGL
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000095------
Georg Brandld7413152009-10-11 21:25:26 +000096
97For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
98
99
100What platform-specific GUI toolkits exist for Python?
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000101========================================================
Georg Brandld7413152009-10-11 21:25:26 +0000102
103`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
104ever-growing set of modules that support the native Mac toolbox calls. The port
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000105supports MacOS X's Carbon libraries.
106
107By installing the `PyObjc Objective-C bridge
108<http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's
109Cocoa libraries. See the documentation that comes with the Mac port.
Georg Brandld7413152009-10-11 21:25:26 +0000110
111:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000112Microsoft Foundation Classes and a Python programming environment
113that's written mostly in Python using the MFC classes.
Georg Brandld7413152009-10-11 21:25:26 +0000114
115
116Tkinter questions
117=================
118
119How do I freeze Tkinter applications?
120-------------------------------------
121
122Freeze is a tool to create stand-alone applications. When freezing Tkinter
123applications, the applications will not be truly stand-alone, as the application
124will still need the Tcl and Tk libraries.
125
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000126One solution is to ship the application with the Tcl and Tk libraries, and point
Georg Brandld7413152009-10-11 21:25:26 +0000127to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
128environment variables.
129
130To get truly stand-alone applications, the Tcl scripts that form the library
131have to be integrated into the application as well. One tool supporting that is
132SAM (stand-alone modules), which is part of the Tix distribution
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000133(http://tix.sourceforge.net/).
134
135Build Tix with SAM enabled, perform the appropriate call to
Georg Brandl60203b42010-10-06 10:11:56 +0000136:c:func:`Tclsam_init`, etc. inside Python's
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000137:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you
138might include the Tix libraries as well).
Georg Brandld7413152009-10-11 21:25:26 +0000139
140
141Can I have Tk events handled while waiting for I/O?
142---------------------------------------------------
143
144Yes, and you don't even need threads! But you'll have to restructure your I/O
Georg Brandl60203b42010-10-06 10:11:56 +0000145code a bit. Tk has the equivalent of Xt's :c:func:`XtAddInput()` call, which allows you
Georg Brandld7413152009-10-11 21:25:26 +0000146to register a callback function which will be called from the Tk mainloop when
147I/O is possible on a file descriptor. Here's what you need::
148
149 from Tkinter import tkinter
150 tkinter.createfilehandler(file, mask, callback)
151
152The file may be a Python file or socket object (actually, anything with a
153fileno() method), or an integer file descriptor. The mask is one of the
154constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
155follows::
156
157 callback(file, mask)
158
159You must unregister the callback when you're done, using ::
160
161 tkinter.deletefilehandler(file)
162
163Note: since you don't know *how many bytes* are available for reading, you can't
164use the Python file object's read or readline methods, since these will insist
165on reading a predefined number of bytes. For sockets, the :meth:`recv` or
166:meth:`recvfrom` methods will work fine; for other files, use
167``os.read(file.fileno(), maxbytecount)``.
168
169
170I can't get key bindings to work in Tkinter: why?
171-------------------------------------------------
172
173An often-heard complaint is that event handlers bound to events with the
174:meth:`bind` method don't get handled even when the appropriate key is pressed.
175
176The most common cause is that the widget to which the binding applies doesn't
177have "keyboard focus". Check out the Tk documentation for the focus command.
178Usually a widget is given the keyboard focus by clicking in it (but not for
179labels; see the takefocus option).