blob: 03177c9586029cff07c623837e88bd3d296a80c4 [file] [log] [blame]
Georg Brandlcb7cb242009-10-27 20:20:38 +00001:tocdepth: 2
2
3==========================
4Graphic User Interface FAQ
5==========================
6
7.. contents::
8
Georg Brandl107690c2010-10-06 07:12:17 +00009.. XXX need review for Python 3.
10
11
Georg Brandlcb7cb242009-10-27 20:20:38 +000012General GUI Questions
13=====================
14
15What platform-independent GUI toolkits exist for Python?
Georg Brandlc62efa82010-07-11 10:41:07 +000016========================================================
Georg Brandlcb7cb242009-10-27 20:20:38 +000017
18Depending on what platform(s) you are aiming at, there are several.
19
20.. XXX check links
21
22Tkinter
Georg Brandlc62efa82010-07-11 10:41:07 +000023-------
Georg Brandlcb7cb242009-10-27 20:20:38 +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
31wxWindows
Georg Brandlc62efa82010-07-11 10:41:07 +000032---------
Georg Brandlcb7cb242009-10-27 20:20:38 +000033
Georg Brandlc62efa82010-07-11 10:41:07 +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 Brandlcb7cb242009-10-27 20:20:38 +000039
Georg Brandlc62efa82010-07-11 10:41:07 +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.
45
46Both 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.
Georg Brandlcb7cb242009-10-27 20:20:38 +000049
50Qt
Georg Brandlc62efa82010-07-11 10:41:07 +000051---
Georg Brandlcb7cb242009-10-27 20:20:38 +000052
53There are bindings available for the Qt toolkit (`PyQt
Georg Brandl628e6f92009-10-27 20:24:45 +000054<http://www.riverbankcomputing.co.uk/software/pyqt/>`_) and for KDE (PyKDE). If
55you're writing open source software, you don't need to pay for PyQt, but if you
56want to write proprietary applications, you must buy a PyQt license from
57`Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4;
58Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech
59<http://www.trolltech.com>`_.
Georg Brandlcb7cb242009-10-27 20:20:38 +000060
61Gtk+
Georg Brandlc62efa82010-07-11 10:41:07 +000062----
Georg Brandlcb7cb242009-10-27 20:20:38 +000063
64PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
65implemented by by James Henstridge; see ftp://ftp.gtk.org/pub/gtk/python/.
66
67FLTK
Georg Brandlc62efa82010-07-11 10:41:07 +000068----
Georg Brandlcb7cb242009-10-27 20:20:38 +000069
70Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
71powerful and mature cross-platform windowing system, are available from `the
72PyFLTK project <http://pyfltk.sourceforge.net>`_.
73
74
75FOX
Georg Brandlc62efa82010-07-11 10:41:07 +000076----
Georg Brandlcb7cb242009-10-27 20:20:38 +000077
78A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
79<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
80and Windows.
81
82
83OpenGL
Georg Brandlc62efa82010-07-11 10:41:07 +000084------
Georg Brandlcb7cb242009-10-27 20:20:38 +000085
86For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
87
88
89What platform-specific GUI toolkits exist for Python?
Georg Brandlc62efa82010-07-11 10:41:07 +000090========================================================
Georg Brandlcb7cb242009-10-27 20:20:38 +000091
92`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
93ever-growing set of modules that support the native Mac toolbox calls. The port
94includes support for MacOS9 and MacOS X's Carbon libraries. By installing the
95`PyObjc Objective-C bridge <http://pyobjc.sourceforge.net>`_, Python programs
96can use MacOS X's Cocoa libraries. See the documentation that comes with the Mac
97port.
98
99:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
100Microsoft Foundation Classes and a Python programming environment using it
101that's written mostly in Python.
102
103
104Tkinter questions
105=================
106
107How do I freeze Tkinter applications?
108-------------------------------------
109
110Freeze is a tool to create stand-alone applications. When freezing Tkinter
111applications, the applications will not be truly stand-alone, as the application
112will still need the Tcl and Tk libraries.
113
114One solution is to ship the application with the tcl and tk libraries, and point
115to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
116environment variables.
117
118To get truly stand-alone applications, the Tcl scripts that form the library
119have to be integrated into the application as well. One tool supporting that is
120SAM (stand-alone modules), which is part of the Tix distribution
121(http://tix.mne.com). Build Tix with SAM enabled, perform the appropriate call
122to Tclsam_init etc inside Python's Modules/tkappinit.c, and link with libtclsam
123and libtksam (you might include the Tix libraries as well).
124
125
126Can I have Tk events handled while waiting for I/O?
127---------------------------------------------------
128
129Yes, and you don't even need threads! But you'll have to restructure your I/O
130code a bit. Tk has the equivalent of Xt's XtAddInput() call, which allows you
131to register a callback function which will be called from the Tk mainloop when
132I/O is possible on a file descriptor. Here's what you need::
133
134 from Tkinter import tkinter
135 tkinter.createfilehandler(file, mask, callback)
136
137The file may be a Python file or socket object (actually, anything with a
138fileno() method), or an integer file descriptor. The mask is one of the
139constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
140follows::
141
142 callback(file, mask)
143
144You must unregister the callback when you're done, using ::
145
146 tkinter.deletefilehandler(file)
147
148Note: since you don't know *how many bytes* are available for reading, you can't
149use the Python file object's read or readline methods, since these will insist
150on reading a predefined number of bytes. For sockets, the :meth:`recv` or
151:meth:`recvfrom` methods will work fine; for other files, use
152``os.read(file.fileno(), maxbytecount)``.
153
154
155I can't get key bindings to work in Tkinter: why?
156-------------------------------------------------
157
158An often-heard complaint is that event handlers bound to events with the
159:meth:`bind` method don't get handled even when the appropriate key is pressed.
160
161The most common cause is that the widget to which the binding applies doesn't
162have "keyboard focus". Check out the Tk documentation for the focus command.
163Usually a widget is given the keyboard focus by clicking in it (but not for
164labels; see the takefocus option).