blob: 57e404966105496ee5c282ac7ef73353c1d3f10f [file] [log] [blame]
Georg Brandlcb7cb242009-10-27 20:20:38 +00001:tocdepth: 2
2
3==========================
4Graphic User Interface FAQ
5==========================
6
7.. contents::
8
9General GUI Questions
10=====================
11
12What platform-independent GUI toolkits exist for Python?
Georg Brandlc62efa82010-07-11 10:41:07 +000013========================================================
Georg Brandlcb7cb242009-10-27 20:20:38 +000014
15Depending on what platform(s) you are aiming at, there are several.
16
17.. XXX check links
18
19Tkinter
Georg Brandlc62efa82010-07-11 10:41:07 +000020-------
Georg Brandlcb7cb242009-10-27 20:20:38 +000021
22Standard builds of Python include an object-oriented interface to the Tcl/Tk
23widget set, called Tkinter. This is probably the easiest to install and use.
24For more info about Tk, including pointers to the source, see the Tcl/Tk home
25page at http://www.tcl.tk. Tcl/Tk is fully portable to the MacOS, Windows, and
26Unix platforms.
27
28wxWindows
Georg Brandlc62efa82010-07-11 10:41:07 +000029---------
Georg Brandlcb7cb242009-10-27 20:20:38 +000030
Georg Brandlc62efa82010-07-11 10:41:07 +000031wxWidgets (http://www.wxwidgets.org) is a free, portable GUI class
32library written in C++ that provides a native look and feel on a
33number of platforms, with Windows, MacOS X, GTK, X11, all listed as
34current stable targets. Language bindings are available for a number
35of languages including Python, Perl, Ruby, etc.
Georg Brandlcb7cb242009-10-27 20:20:38 +000036
Georg Brandlc62efa82010-07-11 10:41:07 +000037wxPython (http://www.wxpython.org) is the Python binding for
38wxwidgets. While it often lags slightly behind the official wxWidgets
39releases, it also offers a number of features via pure Python
40extensions that are not available in other language bindings. There
41is an active wxPython user and developer community.
42
43Both wxWidgets and wxPython are free, open source, software with
44permissive licences that allow their use in commercial products as
45well as in freeware or shareware.
Georg Brandlcb7cb242009-10-27 20:20:38 +000046
47Qt
Georg Brandlc62efa82010-07-11 10:41:07 +000048---
Georg Brandlcb7cb242009-10-27 20:20:38 +000049
50There are bindings available for the Qt toolkit (`PyQt
Georg Brandl628e6f92009-10-27 20:24:45 +000051<http://www.riverbankcomputing.co.uk/software/pyqt/>`_) and for KDE (PyKDE). If
52you're writing open source software, you don't need to pay for PyQt, but if you
53want to write proprietary applications, you must buy a PyQt license from
54`Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4;
55Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech
56<http://www.trolltech.com>`_.
Georg Brandlcb7cb242009-10-27 20:20:38 +000057
58Gtk+
Georg Brandlc62efa82010-07-11 10:41:07 +000059----
Georg Brandlcb7cb242009-10-27 20:20:38 +000060
61PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
62implemented by by James Henstridge; see ftp://ftp.gtk.org/pub/gtk/python/.
63
64FLTK
Georg Brandlc62efa82010-07-11 10:41:07 +000065----
Georg Brandlcb7cb242009-10-27 20:20:38 +000066
67Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
68powerful and mature cross-platform windowing system, are available from `the
69PyFLTK project <http://pyfltk.sourceforge.net>`_.
70
71
72FOX
Georg Brandlc62efa82010-07-11 10:41:07 +000073----
Georg Brandlcb7cb242009-10-27 20:20:38 +000074
75A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
76<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
77and Windows.
78
79
80OpenGL
Georg Brandlc62efa82010-07-11 10:41:07 +000081------
Georg Brandlcb7cb242009-10-27 20:20:38 +000082
83For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
84
85
86What platform-specific GUI toolkits exist for Python?
Georg Brandlc62efa82010-07-11 10:41:07 +000087========================================================
Georg Brandlcb7cb242009-10-27 20:20:38 +000088
89`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
90ever-growing set of modules that support the native Mac toolbox calls. The port
91includes support for MacOS9 and MacOS X's Carbon libraries. By installing the
92`PyObjc Objective-C bridge <http://pyobjc.sourceforge.net>`_, Python programs
93can use MacOS X's Cocoa libraries. See the documentation that comes with the Mac
94port.
95
96:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
97Microsoft Foundation Classes and a Python programming environment using it
98that's written mostly in Python.
99
100
101Tkinter questions
102=================
103
104How do I freeze Tkinter applications?
105-------------------------------------
106
107Freeze is a tool to create stand-alone applications. When freezing Tkinter
108applications, the applications will not be truly stand-alone, as the application
109will still need the Tcl and Tk libraries.
110
111One solution is to ship the application with the tcl and tk libraries, and point
112to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
113environment variables.
114
115To get truly stand-alone applications, the Tcl scripts that form the library
116have to be integrated into the application as well. One tool supporting that is
117SAM (stand-alone modules), which is part of the Tix distribution
118(http://tix.mne.com). Build Tix with SAM enabled, perform the appropriate call
119to Tclsam_init etc inside Python's Modules/tkappinit.c, and link with libtclsam
120and libtksam (you might include the Tix libraries as well).
121
122
123Can I have Tk events handled while waiting for I/O?
124---------------------------------------------------
125
126Yes, and you don't even need threads! But you'll have to restructure your I/O
127code a bit. Tk has the equivalent of Xt's XtAddInput() call, which allows you
128to register a callback function which will be called from the Tk mainloop when
129I/O is possible on a file descriptor. Here's what you need::
130
131 from Tkinter import tkinter
132 tkinter.createfilehandler(file, mask, callback)
133
134The file may be a Python file or socket object (actually, anything with a
135fileno() method), or an integer file descriptor. The mask is one of the
136constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
137follows::
138
139 callback(file, mask)
140
141You must unregister the callback when you're done, using ::
142
143 tkinter.deletefilehandler(file)
144
145Note: since you don't know *how many bytes* are available for reading, you can't
146use the Python file object's read or readline methods, since these will insist
147on reading a predefined number of bytes. For sockets, the :meth:`recv` or
148:meth:`recvfrom` methods will work fine; for other files, use
149``os.read(file.fileno(), maxbytecount)``.
150
151
152I can't get key bindings to work in Tkinter: why?
153-------------------------------------------------
154
155An often-heard complaint is that event handlers bound to events with the
156:meth:`bind` method don't get handled even when the appropriate key is pressed.
157
158The most common cause is that the widget to which the binding applies doesn't
159have "keyboard focus". Check out the Tk documentation for the focus command.
160Usually a widget is given the keyboard focus by clicking in it (but not for
161labels; see the takefocus option).
162
163
164