blob: d3cf7793566043e9783adc5b45eddd7f8c3b25d5 [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
28wxWindows
29'''''''''
30
31wxWindows is a portable GUI class library written in C++ that's a portable
32interface to various platform-specific libraries; wxWidgets is a Python
33interface to wxWindows. wxWindows supports Windows and MacOS; on Unix variants,
34it supports both GTk+ and Motif toolkits. wxWindows preserves the look and feel
35of the underlying graphics toolkit, and there is quite a rich widget set and
36collection of GDI classes. See `the wxWindows page <http://www.wxwindows.org>`_
37for more details.
38
39`wxWidgets <http://wxwidgets.org>`_ is an extension module that wraps many of
40the wxWindows C++ classes, and is quickly gaining popularity amongst Python
41developers. You can get wxWidgets as part of the source or CVS distribution of
42wxWindows, or directly from its home page.
43
44Qt
45'''
46
47There are bindings available for the Qt toolkit (`PyQt
48<http://www.riverbankcomputing.co.uk/pyqt/>`_) and for KDE (PyKDE). If you're
49writing open source software, you don't need to pay for PyQt, but if you want to
50write proprietary applications, you must buy a PyQt license from `Riverbank
51Computing <http://www.riverbankcomputing.co.uk>`_ and a Qt license from
52`Trolltech <http://www.trolltech.com>`_.
53
54Gtk+
55''''
56
57PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
58implemented by by James Henstridge; see ftp://ftp.gtk.org/pub/gtk/python/.
59
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
87includes support for MacOS9 and MacOS X's Carbon libraries. By installing the
88`PyObjc Objective-C bridge <http://pyobjc.sourceforge.net>`_, Python programs
89can use MacOS X's Cocoa libraries. See the documentation that comes with the Mac
90port.
91
92:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
93Microsoft Foundation Classes and a Python programming environment using it
94that's written mostly in Python.
95
96
97Tkinter questions
98=================
99
100How do I freeze Tkinter applications?
101-------------------------------------
102
103Freeze is a tool to create stand-alone applications. When freezing Tkinter
104applications, the applications will not be truly stand-alone, as the application
105will still need the Tcl and Tk libraries.
106
107One solution is to ship the application with the tcl and tk libraries, and point
108to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
109environment variables.
110
111To get truly stand-alone applications, the Tcl scripts that form the library
112have to be integrated into the application as well. One tool supporting that is
113SAM (stand-alone modules), which is part of the Tix distribution
114(http://tix.mne.com). Build Tix with SAM enabled, perform the appropriate call
115to Tclsam_init etc inside Python's Modules/tkappinit.c, and link with libtclsam
116and libtksam (you might include the Tix libraries as well).
117
118
119Can I have Tk events handled while waiting for I/O?
120---------------------------------------------------
121
122Yes, and you don't even need threads! But you'll have to restructure your I/O
123code a bit. Tk has the equivalent of Xt's XtAddInput() call, which allows you
124to register a callback function which will be called from the Tk mainloop when
125I/O is possible on a file descriptor. Here's what you need::
126
127 from Tkinter import tkinter
128 tkinter.createfilehandler(file, mask, callback)
129
130The file may be a Python file or socket object (actually, anything with a
131fileno() method), or an integer file descriptor. The mask is one of the
132constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
133follows::
134
135 callback(file, mask)
136
137You must unregister the callback when you're done, using ::
138
139 tkinter.deletefilehandler(file)
140
141Note: since you don't know *how many bytes* are available for reading, you can't
142use the Python file object's read or readline methods, since these will insist
143on reading a predefined number of bytes. For sockets, the :meth:`recv` or
144:meth:`recvfrom` methods will work fine; for other files, use
145``os.read(file.fileno(), maxbytecount)``.
146
147
148I can't get key bindings to work in Tkinter: why?
149-------------------------------------------------
150
151An often-heard complaint is that event handlers bound to events with the
152:meth:`bind` method don't get handled even when the appropriate key is pressed.
153
154The most common cause is that the widget to which the binding applies doesn't
155have "keyboard focus". Check out the Tk documentation for the focus command.
156Usually a widget is given the keyboard focus by clicking in it (but not for
157labels; see the takefocus option).
158
159
160