blob: 4761b7d334c00e42782a46231b066db64aa5648e [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
Georg Brandl495f7b52009-10-27 15:28:25 +000048<http://www.riverbankcomputing.co.uk/software/pyqt/>`_) and for KDE (PyKDE). If
49you're writing open source software, you don't need to pay for PyQt, but if you
50want to write proprietary applications, you must buy a PyQt license from
51`Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4;
52Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech
53<http://www.trolltech.com>`_.
Georg Brandld7413152009-10-11 21:25:26 +000054
55Gtk+
56''''
57
58PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
59implemented by by James Henstridge; see ftp://ftp.gtk.org/pub/gtk/python/.
60
61FLTK
62''''
63
64Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
65powerful and mature cross-platform windowing system, are available from `the
66PyFLTK project <http://pyfltk.sourceforge.net>`_.
67
68
69FOX
70'''
71
72A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
73<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
74and Windows.
75
76
77OpenGL
78''''''
79
80For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
81
82
83What platform-specific GUI toolkits exist for Python?
84-----------------------------------------------------
85
86`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
87ever-growing set of modules that support the native Mac toolbox calls. The port
88includes support for MacOS9 and MacOS X's Carbon libraries. By installing the
89`PyObjc Objective-C bridge <http://pyobjc.sourceforge.net>`_, Python programs
90can use MacOS X's Cocoa libraries. See the documentation that comes with the Mac
91port.
92
93:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
94Microsoft Foundation Classes and a Python programming environment using it
95that's written mostly in Python.
96
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
108One solution is to ship the application with the tcl and tk libraries, and point
109to 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
115(http://tix.mne.com). Build Tix with SAM enabled, perform the appropriate call
116to Tclsam_init etc inside Python's Modules/tkappinit.c, and link with libtclsam
117and libtksam (you might include the Tix libraries as well).
118
119
120Can I have Tk events handled while waiting for I/O?
121---------------------------------------------------
122
123Yes, and you don't even need threads! But you'll have to restructure your I/O
124code a bit. Tk has the equivalent of Xt's XtAddInput() call, which allows you
125to register a callback function which will be called from the Tk mainloop when
126I/O is possible on a file descriptor. Here's what you need::
127
128 from Tkinter import tkinter
129 tkinter.createfilehandler(file, mask, callback)
130
131The file may be a Python file or socket object (actually, anything with a
132fileno() method), or an integer file descriptor. The mask is one of the
133constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
134follows::
135
136 callback(file, mask)
137
138You must unregister the callback when you're done, using ::
139
140 tkinter.deletefilehandler(file)
141
142Note: since you don't know *how many bytes* are available for reading, you can't
143use the Python file object's read or readline methods, since these will insist
144on reading a predefined number of bytes. For sockets, the :meth:`recv` or
145:meth:`recvfrom` methods will work fine; for other files, use
146``os.read(file.fileno(), maxbytecount)``.
147
148
149I can't get key bindings to work in Tkinter: why?
150-------------------------------------------------
151
152An often-heard complaint is that event handlers bound to events with the
153:meth:`bind` method don't get handled even when the appropriate key is pressed.
154
155The most common cause is that the widget to which the binding applies doesn't
156have "keyboard focus". Check out the Tk documentation for the focus command.
157Usually a widget is given the keyboard focus by clicking in it (but not for
158labels; see the takefocus option).
159
160
161