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