blob: e218896bb110fc96319731329eecb09ceedd69ae [file] [log] [blame]
Georg Brandld7413152009-10-11 21:25:26 +00001:tocdepth: 2
2
3==========================
4Graphic User Interface FAQ
5==========================
6
7.. contents::
8
Georg Brandl62423cb2009-12-19 17:59:59 +00009.. XXX need review for Python 3.
10
11
Georg Brandld7413152009-10-11 21:25:26 +000012General GUI Questions
13=====================
14
15What platform-independent GUI toolkits exist for Python?
16--------------------------------------------------------
17
18Depending on what platform(s) you are aiming at, there are several.
19
20.. XXX check links
21
22Tkinter
23'''''''
24
25Standard builds of Python include an object-oriented interface to the Tcl/Tk
26widget set, called Tkinter. This is probably the easiest to install and use.
27For more info about Tk, including pointers to the source, see the Tcl/Tk home
28page at http://www.tcl.tk. Tcl/Tk is fully portable to the MacOS, Windows, and
29Unix platforms.
30
Benjamin Petersonf6489f92009-11-25 17:46:26 +000031wxWidgets
Georg Brandld7413152009-10-11 21:25:26 +000032'''''''''
33
Benjamin Petersonf6489f92009-11-25 17:46:26 +000034wxWidgets is a GUI class library written in C++ that's a portable
35interface to various platform-specific libraries, and that has a
36Python interface called `wxPython <http://www.wxpython.org>`__.
Georg Brandld7413152009-10-11 21:25:26 +000037
Benjamin Petersonf6489f92009-11-25 17:46:26 +000038wxWidgets preserves the look and feel of the
39underlying graphics toolkit, and has a large set of widgets and
40collection of GDI classes. See `the wxWidgets page
41<http://www.wxwidgets.org>`_ for more details.
42
43wxWidgets supports Windows and MacOS; on Unix variants,
44it supports both GTk+ and Motif toolkits.
Georg Brandld7413152009-10-11 21:25:26 +000045
46Qt
47'''
48
49There are bindings available for the Qt toolkit (`PyQt
Benjamin Petersonf6489f92009-11-25 17:46:26 +000050<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 +000051you're writing open source software, you don't need to pay for PyQt, but if you
52want to write proprietary applications, you must buy a PyQt license from
53`Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4;
54Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech
55<http://www.trolltech.com>`_.
Georg Brandld7413152009-10-11 21:25:26 +000056
57Gtk+
58''''
59
60PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
Benjamin Petersonf6489f92009-11-25 17:46:26 +000061implemented by James Henstridge; see <http://www.pygtk.org>.
Georg Brandld7413152009-10-11 21:25:26 +000062
63FLTK
64''''
65
66Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
67powerful and mature cross-platform windowing system, are available from `the
68PyFLTK project <http://pyfltk.sourceforge.net>`_.
69
70
71FOX
72'''
73
74A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
75<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
76and Windows.
77
78
79OpenGL
80''''''
81
82For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
83
84
85What platform-specific GUI toolkits exist for Python?
86-----------------------------------------------------
87
88`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
89ever-growing set of modules that support the native Mac toolbox calls. The port
Benjamin Petersonf6489f92009-11-25 17:46:26 +000090supports MacOS X's Carbon libraries.
91
92By installing the `PyObjc Objective-C bridge
93<http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's
94Cocoa libraries. See the documentation that comes with the Mac port.
Georg Brandld7413152009-10-11 21:25:26 +000095
96:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
Benjamin Petersonf6489f92009-11-25 17:46:26 +000097Microsoft Foundation Classes and a Python programming environment
98that's written mostly in Python using the MFC classes.
Georg Brandld7413152009-10-11 21:25:26 +000099
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
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000111One solution is to ship the application with the Tcl and Tk libraries, and point
Georg Brandld7413152009-10-11 21:25:26 +0000112to 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
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000118(http://tix.sourceforge.net/).
119
120Build Tix with SAM enabled, perform the appropriate call to
121:cfunc:`Tclsam_init`, etc. inside Python's
122:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you
123might include the Tix libraries as well).
Georg Brandld7413152009-10-11 21:25:26 +0000124
125
126Can I have Tk events handled while waiting for I/O?
127---------------------------------------------------
128
129Yes, and you don't even need threads! But you'll have to restructure your I/O
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000130code a bit. Tk has the equivalent of Xt's :cfunc:`XtAddInput()` call, which allows you
Georg Brandld7413152009-10-11 21:25:26 +0000131to register a callback function which will be called from the Tk mainloop when
132I/O is possible on a file descriptor. Here's what you need::
133
134 from Tkinter import tkinter
135 tkinter.createfilehandler(file, mask, callback)
136
137The file may be a Python file or socket object (actually, anything with a
138fileno() method), or an integer file descriptor. The mask is one of the
139constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
140follows::
141
142 callback(file, mask)
143
144You must unregister the callback when you're done, using ::
145
146 tkinter.deletefilehandler(file)
147
148Note: since you don't know *how many bytes* are available for reading, you can't
149use the Python file object's read or readline methods, since these will insist
150on reading a predefined number of bytes. For sockets, the :meth:`recv` or
151:meth:`recvfrom` methods will work fine; for other files, use
152``os.read(file.fileno(), maxbytecount)``.
153
154
155I can't get key bindings to work in Tkinter: why?
156-------------------------------------------------
157
158An often-heard complaint is that event handlers bound to events with the
159:meth:`bind` method don't get handled even when the appropriate key is pressed.
160
161The most common cause is that the widget to which the binding applies doesn't
162have "keyboard focus". Check out the Tk documentation for the focus command.
163Usually a widget is given the keyboard focus by clicking in it (but not for
164labels; see the takefocus option).