blob: 5827f28c3bdc4854a8c7a04ff68fe4cec56dee3e [file] [log] [blame]
Georg Brandld7413152009-10-11 21:25:26 +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 Brandld7413152009-10-11 21:25:26 +000010
Georg Brandl62423cb2009-12-19 17:59:59 +000011.. XXX need review for Python 3.
12
13
Georg Brandld7413152009-10-11 21:25:26 +000014General GUI Questions
15=====================
16
17What platform-independent GUI toolkits exist for Python?
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000018========================================================
Georg Brandld7413152009-10-11 21:25:26 +000019
Antoine Pitroueb50ba52011-02-05 10:43:57 +000020Depending on what platform(s) you are aiming at, there are several. Some
21of them haven't been ported to Python 3 yet. At least `Tkinter`_ and `Qt`_
22are known to be Python 3-compatible.
Georg Brandld7413152009-10-11 21:25:26 +000023
24.. XXX check links
25
26Tkinter
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000027-------
Georg Brandld7413152009-10-11 21:25:26 +000028
29Standard builds of Python include an object-oriented interface to the Tcl/Tk
Antoine Pitroueb50ba52011-02-05 10:43:57 +000030widget set, called :ref:`tkinter <Tkinter>`. This is probably the easiest to
31install (since it comes included with most
32`binary distributions <http://www.python.org/download/>`_ of Python) and use.
33For more info about Tk, including pointers to the source, see the
34`Tcl/Tk home page <http://www.tcl.tk>`_. Tcl/Tk is fully portable to the
35MacOS, Windows, and Unix platforms.
Georg Brandld7413152009-10-11 21:25:26 +000036
Benjamin Petersonf6489f92009-11-25 17:46:26 +000037wxWidgets
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000038---------
Georg Brandld7413152009-10-11 21:25:26 +000039
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000040wxWidgets (http://www.wxwidgets.org) is a free, portable GUI class
41library written in C++ that provides a native look and feel on a
42number of platforms, with Windows, MacOS X, GTK, X11, all listed as
43current stable targets. Language bindings are available for a number
44of languages including Python, Perl, Ruby, etc.
Georg Brandld7413152009-10-11 21:25:26 +000045
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000046wxPython (http://www.wxpython.org) is the Python binding for
47wxwidgets. While it often lags slightly behind the official wxWidgets
48releases, it also offers a number of features via pure Python
49extensions that are not available in other language bindings. There
50is an active wxPython user and developer community.
Benjamin Petersonf6489f92009-11-25 17:46:26 +000051
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000052Both wxWidgets and wxPython are free, open source, software with
53permissive licences that allow their use in commercial products as
54well as in freeware or shareware.
55
Georg Brandld7413152009-10-11 21:25:26 +000056
57Qt
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000058---
Georg Brandld7413152009-10-11 21:25:26 +000059
Antoine Pitroueb50ba52011-02-05 10:43:57 +000060There are bindings available for the Qt toolkit (using either `PyQt
61<http://www.riverbankcomputing.co.uk/software/pyqt/>`_ or `PySide
62<http://www.pyside.org/>`_) and for KDE (`PyKDE <http://www.riverbankcomputing.co.uk/software/pykde/intro>`__).
63PyQt is currently more mature than PySide, but you must buy a PyQt license from
64`Riverbank Computing <http://www.riverbankcomputing.co.uk/software/pyqt/license>`_
65if you want to write proprietary applications. PySide is free for all applications.
66
67Qt 4.5 upwards is licensed under the LGPL license; also, commercial licenses
68are available from `Nokia <http://qt.nokia.com/>`_.
Georg Brandld7413152009-10-11 21:25:26 +000069
70Gtk+
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000071----
Georg Brandld7413152009-10-11 21:25:26 +000072
Antoine Pitrou7462e872011-12-03 20:59:24 +010073The `GObject introspection bindings <https://live.gnome.org/PyGObject>`_
74for Python allow you to write GTK+ 3 applications. There is also a
75`Python GTK+ 3 Tutorial <http://python-gtk-3-tutorial.readthedocs.org/en/latest/>`_.
76
77The older PyGtk bindings for the `Gtk+ 2 toolkit <http://www.gtk.org>`_ have
78been implemented by James Henstridge; see <http://www.pygtk.org>.
Georg Brandld7413152009-10-11 21:25:26 +000079
80FLTK
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000081----
Georg Brandld7413152009-10-11 21:25:26 +000082
83Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
84powerful and mature cross-platform windowing system, are available from `the
85PyFLTK project <http://pyfltk.sourceforge.net>`_.
86
87
88FOX
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000089----
Georg Brandld7413152009-10-11 21:25:26 +000090
91A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
92<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
93and Windows.
94
95
96OpenGL
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000097------
Georg Brandld7413152009-10-11 21:25:26 +000098
99For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
100
101
102What platform-specific GUI toolkits exist for Python?
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000103========================================================
Georg Brandld7413152009-10-11 21:25:26 +0000104
105`The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and
106ever-growing set of modules that support the native Mac toolbox calls. The port
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000107supports MacOS X's Carbon libraries.
108
109By installing the `PyObjc Objective-C bridge
110<http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's
111Cocoa libraries. See the documentation that comes with the Mac port.
Georg Brandld7413152009-10-11 21:25:26 +0000112
113:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000114Microsoft Foundation Classes and a Python programming environment
115that's written mostly in Python using the MFC classes.
Georg Brandld7413152009-10-11 21:25:26 +0000116
117
118Tkinter questions
119=================
120
121How do I freeze Tkinter applications?
122-------------------------------------
123
124Freeze is a tool to create stand-alone applications. When freezing Tkinter
125applications, the applications will not be truly stand-alone, as the application
126will still need the Tcl and Tk libraries.
127
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000128One solution is to ship the application with the Tcl and Tk libraries, and point
Georg Brandld7413152009-10-11 21:25:26 +0000129to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
130environment variables.
131
132To get truly stand-alone applications, the Tcl scripts that form the library
133have to be integrated into the application as well. One tool supporting that is
134SAM (stand-alone modules), which is part of the Tix distribution
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000135(http://tix.sourceforge.net/).
136
137Build Tix with SAM enabled, perform the appropriate call to
Georg Brandl60203b42010-10-06 10:11:56 +0000138:c:func:`Tclsam_init`, etc. inside Python's
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000139:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you
140might include the Tix libraries as well).
Georg Brandld7413152009-10-11 21:25:26 +0000141
142
143Can I have Tk events handled while waiting for I/O?
144---------------------------------------------------
145
146Yes, and you don't even need threads! But you'll have to restructure your I/O
Georg Brandl60203b42010-10-06 10:11:56 +0000147code a bit. Tk has the equivalent of Xt's :c:func:`XtAddInput()` call, which allows you
Georg Brandld7413152009-10-11 21:25:26 +0000148to register a callback function which will be called from the Tk mainloop when
149I/O is possible on a file descriptor. Here's what you need::
150
151 from Tkinter import tkinter
152 tkinter.createfilehandler(file, mask, callback)
153
154The file may be a Python file or socket object (actually, anything with a
155fileno() method), or an integer file descriptor. The mask is one of the
156constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as
157follows::
158
159 callback(file, mask)
160
161You must unregister the callback when you're done, using ::
162
163 tkinter.deletefilehandler(file)
164
165Note: since you don't know *how many bytes* are available for reading, you can't
166use the Python file object's read or readline methods, since these will insist
167on reading a predefined number of bytes. For sockets, the :meth:`recv` or
168:meth:`recvfrom` methods will work fine; for other files, use
169``os.read(file.fileno(), maxbytecount)``.
170
171
172I can't get key bindings to work in Tkinter: why?
173-------------------------------------------------
174
175An often-heard complaint is that event handlers bound to events with the
176:meth:`bind` method don't get handled even when the appropriate key is pressed.
177
178The most common cause is that the widget to which the binding applies doesn't
179have "keyboard focus". Check out the Tk documentation for the focus command.
180Usually a widget is given the keyboard focus by clicking in it (but not for
181labels; see the takefocus option).