blob: fd6ca0cefd68c317bb49945dc98bc0d412bfe10d [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
18Depending on what platform(s) you are aiming at, there are several.
19
20.. XXX check links
21
22Tkinter
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000023-------
Georg Brandld7413152009-10-11 21:25:26 +000024
25Standard builds of Python include an object-oriented interface to the Tcl/Tk
26widget set, called Tkinter. This is probably the easiest to install and use.
27For more info about Tk, including pointers to the source, see the Tcl/Tk home
28page at http://www.tcl.tk. Tcl/Tk is fully portable to the MacOS, Windows, and
29Unix platforms.
30
Benjamin Petersonf6489f92009-11-25 17:46:26 +000031wxWidgets
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000032---------
Georg Brandld7413152009-10-11 21:25:26 +000033
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000034wxWidgets (http://www.wxwidgets.org) is a free, portable GUI class
35library written in C++ that provides a native look and feel on a
36number of platforms, with Windows, MacOS X, GTK, X11, all listed as
37current stable targets. Language bindings are available for a number
38of languages including Python, Perl, Ruby, etc.
Georg Brandld7413152009-10-11 21:25:26 +000039
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000040wxPython (http://www.wxpython.org) is the Python binding for
41wxwidgets. While it often lags slightly behind the official wxWidgets
42releases, it also offers a number of features via pure Python
43extensions that are not available in other language bindings. There
44is an active wxPython user and developer community.
Benjamin Petersonf6489f92009-11-25 17:46:26 +000045
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000046Both wxWidgets and wxPython are free, open source, software with
47permissive licences that allow their use in commercial products as
48well as in freeware or shareware.
49
Georg Brandld7413152009-10-11 21:25:26 +000050
51Qt
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000052---
Georg Brandld7413152009-10-11 21:25:26 +000053
54There are bindings available for the Qt toolkit (`PyQt
Benjamin Petersonf6489f92009-11-25 17:46:26 +000055<http://www.riverbankcomputing.co.uk/software/pyqt/>`_) and for KDE (`PyKDE <http://www.riverbankcomputing.co.uk/software/pykde/intro>`__). If
Georg Brandl495f7b52009-10-27 15:28:25 +000056you're writing open source software, you don't need to pay for PyQt, but if you
57want to write proprietary applications, you must buy a PyQt license from
58`Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4;
59Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech
60<http://www.trolltech.com>`_.
Georg Brandld7413152009-10-11 21:25:26 +000061
62Gtk+
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000063----
Georg Brandld7413152009-10-11 21:25:26 +000064
65PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
Benjamin Petersonf6489f92009-11-25 17:46:26 +000066implemented by James Henstridge; see <http://www.pygtk.org>.
Georg Brandld7413152009-10-11 21:25:26 +000067
68FLTK
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000069----
Georg Brandld7413152009-10-11 21:25:26 +000070
71Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
72powerful and mature cross-platform windowing system, are available from `the
73PyFLTK project <http://pyfltk.sourceforge.net>`_.
74
75
76FOX
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000077----
Georg Brandld7413152009-10-11 21:25:26 +000078
79A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
80<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
81and Windows.
82
83
84OpenGL
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000085------
Georg Brandld7413152009-10-11 21:25:26 +000086
87For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
88
89
90What platform-specific GUI toolkits exist for Python?
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000091========================================================
Georg Brandld7413152009-10-11 21:25:26 +000092
93`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
94ever-growing set of modules that support the native Mac toolbox calls. The port
Benjamin Petersonf6489f92009-11-25 17:46:26 +000095supports MacOS X's Carbon libraries.
96
97By installing the `PyObjc Objective-C bridge
98<http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's
99Cocoa libraries. See the documentation that comes with the Mac port.
Georg Brandld7413152009-10-11 21:25:26 +0000100
101:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000102Microsoft Foundation Classes and a Python programming environment
103that's written mostly in Python using the MFC classes.
Georg Brandld7413152009-10-11 21:25:26 +0000104
105
106Tkinter questions
107=================
108
109How do I freeze Tkinter applications?
110-------------------------------------
111
112Freeze is a tool to create stand-alone applications. When freezing Tkinter
113applications, the applications will not be truly stand-alone, as the application
114will still need the Tcl and Tk libraries.
115
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000116One solution is to ship the application with the Tcl and Tk libraries, and point
Georg Brandld7413152009-10-11 21:25:26 +0000117to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
118environment variables.
119
120To get truly stand-alone applications, the Tcl scripts that form the library
121have to be integrated into the application as well. One tool supporting that is
122SAM (stand-alone modules), which is part of the Tix distribution
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000123(http://tix.sourceforge.net/).
124
125Build Tix with SAM enabled, perform the appropriate call to
126:cfunc:`Tclsam_init`, etc. inside Python's
127:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you
128might include the Tix libraries as well).
Georg Brandld7413152009-10-11 21:25:26 +0000129
130
131Can I have Tk events handled while waiting for I/O?
132---------------------------------------------------
133
134Yes, and you don't even need threads! But you'll have to restructure your I/O
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000135code a bit. Tk has the equivalent of Xt's :cfunc:`XtAddInput()` call, which allows you
Georg Brandld7413152009-10-11 21:25:26 +0000136to register a callback function which will be called from the Tk mainloop when
137I/O is possible on a file descriptor. Here's what you need::
138
139 from Tkinter import tkinter
140 tkinter.createfilehandler(file, mask, callback)
141
142The file may be a Python file or socket object (actually, anything with a
143fileno() method), or an integer file descriptor. The mask is one of the
144constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
145follows::
146
147 callback(file, mask)
148
149You must unregister the callback when you're done, using ::
150
151 tkinter.deletefilehandler(file)
152
153Note: since you don't know *how many bytes* are available for reading, you can't
154use the Python file object's read or readline methods, since these will insist
155on reading a predefined number of bytes. For sockets, the :meth:`recv` or
156:meth:`recvfrom` methods will work fine; for other files, use
157``os.read(file.fileno(), maxbytecount)``.
158
159
160I can't get key bindings to work in Tkinter: why?
161-------------------------------------------------
162
163An often-heard complaint is that event handlers bound to events with the
164:meth:`bind` method don't get handled even when the appropriate key is pressed.
165
166The most common cause is that the widget to which the binding applies doesn't
167have "keyboard focus". Check out the Tk documentation for the focus command.
168Usually a widget is given the keyboard focus by clicking in it (but not for
169labels; see the takefocus option).