blob: 1f2ae0989ccb37ae0db316ba5b91c534e9702ecd [file] [log] [blame]
Georg Brandld7413152009-10-11 21:25:26 +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
Benjamin Petersonf6489f92009-11-25 17:46:26 +000028wxWidgets
Georg Brandld7413152009-10-11 21:25:26 +000029'''''''''
30
Benjamin Petersonf6489f92009-11-25 17:46:26 +000031wxWidgets is a GUI class library written in C++ that's a portable
32interface to various platform-specific libraries, and that has a
33Python interface called `wxPython <http://www.wxpython.org>`__.
Georg Brandld7413152009-10-11 21:25:26 +000034
Benjamin Petersonf6489f92009-11-25 17:46:26 +000035wxWidgets preserves the look and feel of the
36underlying graphics toolkit, and has a large set of widgets and
37collection of GDI classes. See `the wxWidgets page
38<http://www.wxwidgets.org>`_ for more details.
39
40wxWidgets supports Windows and MacOS; on Unix variants,
41it supports both GTk+ and Motif toolkits.
Georg Brandld7413152009-10-11 21:25:26 +000042
43Qt
44'''
45
46There are bindings available for the Qt toolkit (`PyQt
Benjamin Petersonf6489f92009-11-25 17:46:26 +000047<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 +000048you're writing open source software, you don't need to pay for PyQt, but if you
49want to write proprietary applications, you must buy a PyQt license from
50`Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4;
51Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech
52<http://www.trolltech.com>`_.
Georg Brandld7413152009-10-11 21:25:26 +000053
54Gtk+
55''''
56
57PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
Benjamin Petersonf6489f92009-11-25 17:46:26 +000058implemented by James Henstridge; see <http://www.pygtk.org>.
Georg Brandld7413152009-10-11 21:25:26 +000059
60FLTK
61''''
62
63Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
64powerful and mature cross-platform windowing system, are available from `the
65PyFLTK project <http://pyfltk.sourceforge.net>`_.
66
67
68FOX
69'''
70
71A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
72<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
73and Windows.
74
75
76OpenGL
77''''''
78
79For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
80
81
82What platform-specific GUI toolkits exist for Python?
83-----------------------------------------------------
84
85`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
86ever-growing set of modules that support the native Mac toolbox calls. The port
Benjamin Petersonf6489f92009-11-25 17:46:26 +000087supports MacOS X's Carbon libraries.
88
89By installing the `PyObjc Objective-C bridge
90<http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's
91Cocoa libraries. See the documentation that comes with the Mac port.
Georg Brandld7413152009-10-11 21:25:26 +000092
93:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
Benjamin Petersonf6489f92009-11-25 17:46:26 +000094Microsoft Foundation Classes and a Python programming environment
95that's written mostly in Python using the MFC classes.
Georg Brandld7413152009-10-11 21:25:26 +000096
97
98Tkinter questions
99=================
100
101How do I freeze Tkinter applications?
102-------------------------------------
103
104Freeze is a tool to create stand-alone applications. When freezing Tkinter
105applications, the applications will not be truly stand-alone, as the application
106will still need the Tcl and Tk libraries.
107
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000108One solution is to ship the application with the Tcl and Tk libraries, and point
Georg Brandld7413152009-10-11 21:25:26 +0000109to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
110environment variables.
111
112To get truly stand-alone applications, the Tcl scripts that form the library
113have to be integrated into the application as well. One tool supporting that is
114SAM (stand-alone modules), which is part of the Tix distribution
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000115(http://tix.sourceforge.net/).
116
117Build Tix with SAM enabled, perform the appropriate call to
118:cfunc:`Tclsam_init`, etc. inside Python's
119:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you
120might include the Tix libraries as well).
Georg Brandld7413152009-10-11 21:25:26 +0000121
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
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000127code a bit. Tk has the equivalent of Xt's :cfunc:`XtAddInput()` call, which allows you
Georg Brandld7413152009-10-11 21:25:26 +0000128to 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