blob: d37f4809c7205cf3a420992cf25fd83d4d699118 [file] [log] [blame]
Georg Brandl6728c5a2009-10-11 18:31:23 +00001:tocdepth: 2
2
3==========================
4Graphic User Interface FAQ
5==========================
6
7.. contents::
8
Georg Brandl6728c5a2009-10-11 18:31:23 +00009What platform-independent GUI toolkits exist for Python?
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000010========================================================
Georg Brandl6728c5a2009-10-11 18:31:23 +000011
12Depending on what platform(s) you are aiming at, there are several.
13
14.. XXX check links
15
16Tkinter
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000017-------
Georg Brandl6728c5a2009-10-11 18:31:23 +000018
19Standard builds of Python include an object-oriented interface to the Tcl/Tk
20widget set, called Tkinter. This is probably the easiest to install and use.
21For more info about Tk, including pointers to the source, see the Tcl/Tk home
22page at http://www.tcl.tk. Tcl/Tk is fully portable to the MacOS, Windows, and
23Unix platforms.
24
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000025wxWidgets
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000026---------
Georg Brandl6728c5a2009-10-11 18:31:23 +000027
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +000028wxWidgets (http://www.wxwidgets.org) is a free, portable GUI class
29library written in C++ that provides a native look and feel on a
30number of platforms, with Windows, MacOS X, GTK, X11, all listed as
31current stable targets. Language bindings are available for a number
32of languages including Python, Perl, Ruby, etc.
Georg Brandl6728c5a2009-10-11 18:31:23 +000033
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +000034wxPython (http://www.wxpython.org) is the Python binding for
35wxwidgets. While it often lags slightly behind the official wxWidgets
36releases, it also offers a number of features via pure Python
37extensions that are not available in other language bindings. There
38is an active wxPython user and developer community.
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000039
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +000040Both wxWidgets and wxPython are free, open source, software with
41permissive licences that allow their use in commercial products as
42well as in freeware or shareware.
43
Georg Brandl6728c5a2009-10-11 18:31:23 +000044
45Qt
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000046---
Georg Brandl6728c5a2009-10-11 18:31:23 +000047
48There are bindings available for the Qt toolkit (`PyQt
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000049<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 +000050you're writing open source software, you don't need to pay for PyQt, but if you
51want to write proprietary applications, you must buy a PyQt license from
52`Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4;
53Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech
54<http://www.trolltech.com>`_.
Georg Brandl6728c5a2009-10-11 18:31:23 +000055
56Gtk+
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000057----
Georg Brandl6728c5a2009-10-11 18:31:23 +000058
59PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000060implemented by James Henstridge; see <http://www.pygtk.org>.
Georg Brandl6728c5a2009-10-11 18:31:23 +000061
62FLTK
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000063----
Georg Brandl6728c5a2009-10-11 18:31:23 +000064
65Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
66powerful and mature cross-platform windowing system, are available from `the
67PyFLTK project <http://pyfltk.sourceforge.net>`_.
68
69
70FOX
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000071----
Georg Brandl6728c5a2009-10-11 18:31:23 +000072
73A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
74<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
75and Windows.
76
77
78OpenGL
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000079------
Georg Brandl6728c5a2009-10-11 18:31:23 +000080
81For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
82
83
84What platform-specific GUI toolkits exist for Python?
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000085========================================================
Georg Brandl6728c5a2009-10-11 18:31:23 +000086
87`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
88ever-growing set of modules that support the native Mac toolbox calls. The port
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000089supports MacOS X's Carbon libraries.
90
91By installing the `PyObjc Objective-C bridge
92<http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's
93Cocoa libraries. See the documentation that comes with the Mac port.
Georg Brandl6728c5a2009-10-11 18:31:23 +000094
95:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000096Microsoft Foundation Classes and a Python programming environment
97that's written mostly in Python using the MFC classes.
Georg Brandl6728c5a2009-10-11 18:31:23 +000098
99
100Tkinter questions
101=================
102
103How do I freeze Tkinter applications?
104-------------------------------------
105
106Freeze is a tool to create stand-alone applications. When freezing Tkinter
107applications, the applications will not be truly stand-alone, as the application
108will still need the Tcl and Tk libraries.
109
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +0000110One solution is to ship the application with the Tcl and Tk libraries, and point
Georg Brandl6728c5a2009-10-11 18:31:23 +0000111to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
112environment variables.
113
114To get truly stand-alone applications, the Tcl scripts that form the library
115have to be integrated into the application as well. One tool supporting that is
116SAM (stand-alone modules), which is part of the Tix distribution
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +0000117(http://tix.sourceforge.net/).
118
119Build Tix with SAM enabled, perform the appropriate call to
120:cfunc:`Tclsam_init`, etc. inside Python's
121:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you
122might include the Tix libraries as well).
Georg Brandl6728c5a2009-10-11 18:31:23 +0000123
124
125Can I have Tk events handled while waiting for I/O?
126---------------------------------------------------
127
128Yes, and you don't even need threads! But you'll have to restructure your I/O
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +0000129code a bit. Tk has the equivalent of Xt's :cfunc:`XtAddInput()` call, which allows you
Georg Brandl6728c5a2009-10-11 18:31:23 +0000130to register a callback function which will be called from the Tk mainloop when
131I/O is possible on a file descriptor. Here's what you need::
132
133 from Tkinter import tkinter
134 tkinter.createfilehandler(file, mask, callback)
135
136The file may be a Python file or socket object (actually, anything with a
137fileno() method), or an integer file descriptor. The mask is one of the
138constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
139follows::
140
141 callback(file, mask)
142
143You must unregister the callback when you're done, using ::
144
145 tkinter.deletefilehandler(file)
146
147Note: since you don't know *how many bytes* are available for reading, you can't
148use the Python file object's read or readline methods, since these will insist
149on reading a predefined number of bytes. For sockets, the :meth:`recv` or
150:meth:`recvfrom` methods will work fine; for other files, use
151``os.read(file.fileno(), maxbytecount)``.
152
153
154I can't get key bindings to work in Tkinter: why?
155-------------------------------------------------
156
157An often-heard complaint is that event handlers bound to events with the
158:meth:`bind` method don't get handled even when the appropriate key is pressed.
159
160The most common cause is that the widget to which the binding applies doesn't
161have "keyboard focus". Check out the Tk documentation for the focus command.
162Usually a widget is given the keyboard focus by clicking in it (but not for
163labels; see the takefocus option).
164
165
166