blob: 8041614197287c3046951a3392e11eb6c0679187 [file] [log] [blame]
Georg Brandl6728c5a2009-10-11 18:31:23 +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?
13--------------------------------------------------------
14
15Depending on what platform(s) you are aiming at, there are several.
16
17.. XXX check links
18
19Tkinter
20'''''''
21
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
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000028wxWidgets
Georg Brandl6728c5a2009-10-11 18:31:23 +000029'''''''''
30
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +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 Brandl6728c5a2009-10-11 18:31:23 +000036
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +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.
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000042
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +000043Both 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.
46
Georg Brandl6728c5a2009-10-11 18:31:23 +000047
48Qt
49'''
50
51There are bindings available for the Qt toolkit (`PyQt
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000052<http://www.riverbankcomputing.co.uk/software/pyqt/>`_) and for KDE (`PyKDE <http://www.riverbankcomputing.co.uk/software/pykde/intro>`__). If
Georg Brandla4314c22009-10-11 20:16:16 +000053you're writing open source software, you don't need to pay for PyQt, but if you
54want to write proprietary applications, you must buy a PyQt license from
55`Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4;
56Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech
57<http://www.trolltech.com>`_.
Georg Brandl6728c5a2009-10-11 18:31:23 +000058
59Gtk+
60''''
61
62PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000063implemented by James Henstridge; see <http://www.pygtk.org>.
Georg Brandl6728c5a2009-10-11 18:31:23 +000064
65FLTK
66''''
67
68Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
69powerful and mature cross-platform windowing system, are available from `the
70PyFLTK project <http://pyfltk.sourceforge.net>`_.
71
72
73FOX
74'''
75
76A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
77<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
78and Windows.
79
80
81OpenGL
82''''''
83
84For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
85
86
87What platform-specific GUI toolkits exist for Python?
88-----------------------------------------------------
89
90`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
91ever-growing set of modules that support the native Mac toolbox calls. The port
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000092supports MacOS X's Carbon libraries.
93
94By installing the `PyObjc Objective-C bridge
95<http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's
96Cocoa libraries. See the documentation that comes with the Mac port.
Georg Brandl6728c5a2009-10-11 18:31:23 +000097
98:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000099Microsoft Foundation Classes and a Python programming environment
100that's written mostly in Python using the MFC classes.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000101
102
103Tkinter questions
104=================
105
106How do I freeze Tkinter applications?
107-------------------------------------
108
109Freeze is a tool to create stand-alone applications. When freezing Tkinter
110applications, the applications will not be truly stand-alone, as the application
111will still need the Tcl and Tk libraries.
112
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +0000113One solution is to ship the application with the Tcl and Tk libraries, and point
Georg Brandl6728c5a2009-10-11 18:31:23 +0000114to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
115environment variables.
116
117To get truly stand-alone applications, the Tcl scripts that form the library
118have to be integrated into the application as well. One tool supporting that is
119SAM (stand-alone modules), which is part of the Tix distribution
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +0000120(http://tix.sourceforge.net/).
121
122Build Tix with SAM enabled, perform the appropriate call to
123:cfunc:`Tclsam_init`, etc. inside Python's
124:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you
125might include the Tix libraries as well).
Georg Brandl6728c5a2009-10-11 18:31:23 +0000126
127
128Can I have Tk events handled while waiting for I/O?
129---------------------------------------------------
130
131Yes, and you don't even need threads! But you'll have to restructure your I/O
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +0000132code a bit. Tk has the equivalent of Xt's :cfunc:`XtAddInput()` call, which allows you
Georg Brandl6728c5a2009-10-11 18:31:23 +0000133to register a callback function which will be called from the Tk mainloop when
134I/O is possible on a file descriptor. Here's what you need::
135
136 from Tkinter import tkinter
137 tkinter.createfilehandler(file, mask, callback)
138
139The file may be a Python file or socket object (actually, anything with a
140fileno() method), or an integer file descriptor. The mask is one of the
141constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
142follows::
143
144 callback(file, mask)
145
146You must unregister the callback when you're done, using ::
147
148 tkinter.deletefilehandler(file)
149
150Note: since you don't know *how many bytes* are available for reading, you can't
151use the Python file object's read or readline methods, since these will insist
152on reading a predefined number of bytes. For sockets, the :meth:`recv` or
153:meth:`recvfrom` methods will work fine; for other files, use
154``os.read(file.fileno(), maxbytecount)``.
155
156
157I can't get key bindings to work in Tkinter: why?
158-------------------------------------------------
159
160An often-heard complaint is that event handlers bound to events with the
161:meth:`bind` method don't get handled even when the appropriate key is pressed.
162
163The most common cause is that the widget to which the binding applies doesn't
164have "keyboard focus". Check out the Tk documentation for the focus command.
165Usually a widget is given the keyboard focus by clicking in it (but not for
166labels; see the takefocus option).
167
168
169