| 1 |
:tocdepth: 2 |
| 2 |
|
| 3 |
========================== |
| 4 |
Graphic User Interface FAQ |
| 5 |
========================== |
| 6 |
|
| 7 |
.. only:: html |
| 8 |
|
| 9 |
.. contents:: |
| 10 |
|
| 11 |
What platform-independent GUI toolkits exist for Python? |
| 12 |
======================================================== |
| 13 |
|
| 14 |
Depending on what platform(s) you are aiming at, there are several. |
| 15 |
|
| 16 |
.. XXX check links |
| 17 |
|
| 18 |
Tkinter |
| 19 |
------- |
| 20 |
|
| 21 |
Standard builds of Python include an object-oriented interface to the Tcl/Tk |
| 22 |
widget set, called Tkinter. This is probably the easiest to install and use. |
| 23 |
For more info about Tk, including pointers to the source, see the Tcl/Tk home |
| 24 |
page at https://www.tcl.tk. Tcl/Tk is fully portable to the Mac OS X, Windows, |
| 25 |
and Unix platforms. |
| 26 |
|
| 27 |
wxWidgets |
| 28 |
--------- |
| 29 |
|
| 30 |
wxWidgets (https://www.wxwidgets.org) is a free, portable GUI class |
| 31 |
library written in C++ that provides a native look and feel on a |
| 32 |
number of platforms, with Windows, Mac OS X, GTK, X11, all listed as |
| 33 |
current stable targets. Language bindings are available for a number |
| 34 |
of languages including Python, Perl, Ruby, etc. |
| 35 |
|
| 36 |
wxPython (http://www.wxpython.org) is the Python binding for |
| 37 |
wxwidgets. While it often lags slightly behind the official wxWidgets |
| 38 |
releases, it also offers a number of features via pure Python |
| 39 |
extensions that are not available in other language bindings. There |
| 40 |
is an active wxPython user and developer community. |
| 41 |
|
| 42 |
Both wxWidgets and wxPython are free, open source, software with |
| 43 |
permissive licences that allow their use in commercial products as |
| 44 |
well as in freeware or shareware. |
| 45 |
|
| 46 |
|
| 47 |
Qt |
| 48 |
--- |
| 49 |
|
| 50 |
There are bindings available for the Qt toolkit (using either `PyQt |
| 51 |
<https://riverbankcomputing.com/software/pyqt/intro>`_ or `PySide |
| 52 |
<https://wiki.qt.io/PySide>`_) and for KDE (`PyKDE4 <https://techbase.kde.org/Languages/Python/Using_PyKDE_4>`__). |
| 53 |
PyQt is currently more mature than PySide, but you must buy a PyQt license from |
| 54 |
`Riverbank Computing <https://www.riverbankcomputing.com/commercial/license-faq>`_ |
| 55 |
if you want to write proprietary applications. PySide is free for all applications. |
| 56 |
|
| 57 |
Qt 4.5 upwards is licensed under the LGPL license; also, commercial licenses |
| 58 |
are available from `The Qt Company <https://www.qt.io/licensing/>`_. |
| 59 |
|
| 60 |
Gtk+ |
| 61 |
---- |
| 62 |
|
| 63 |
PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been |
| 64 |
implemented by James Henstridge; see <http://www.pygtk.org>. |
| 65 |
|
| 66 |
FLTK |
| 67 |
---- |
| 68 |
|
| 69 |
Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet |
| 70 |
powerful and mature cross-platform windowing system, are available from `the |
| 71 |
PyFLTK project <http://pyfltk.sourceforge.net>`_. |
| 72 |
|
| 73 |
OpenGL |
| 74 |
------ |
| 75 |
|
| 76 |
For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_. |
| 77 |
|
| 78 |
|
| 79 |
What platform-specific GUI toolkits exist for Python? |
| 80 |
======================================================== |
| 81 |
|
| 82 |
By installing the `PyObjc Objective-C bridge |
| 83 |
<https://pythonhosted.org/pyobjc/>`_, Python programs can use Mac OS X's |
| 84 |
Cocoa libraries. |
| 85 |
|
| 86 |
:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the |
| 87 |
Microsoft Foundation Classes and a Python programming environment |
| 88 |
that's written mostly in Python using the MFC classes. |
| 89 |
|
| 90 |
|
| 91 |
Tkinter questions |
| 92 |
================= |
| 93 |
|
| 94 |
How do I freeze Tkinter applications? |
| 95 |
------------------------------------- |
| 96 |
|
| 97 |
Freeze is a tool to create stand-alone applications. When freezing Tkinter |
| 98 |
applications, the applications will not be truly stand-alone, as the application |
| 99 |
will still need the Tcl and Tk libraries. |
| 100 |
|
| 101 |
One solution is to ship the application with the Tcl and Tk libraries, and point |
| 102 |
to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY` |
| 103 |
environment variables. |
| 104 |
|
| 105 |
To get truly stand-alone applications, the Tcl scripts that form the library |
| 106 |
have to be integrated into the application as well. One tool supporting that is |
| 107 |
SAM (stand-alone modules), which is part of the Tix distribution |
| 108 |
(http://tix.sourceforge.net/). |
| 109 |
|
| 110 |
Build Tix with SAM enabled, perform the appropriate call to |
| 111 |
:c:func:`Tclsam_init`, etc. inside Python's |
| 112 |
:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you |
| 113 |
might include the Tix libraries as well). |
| 114 |
|
| 115 |
|
| 116 |
Can I have Tk events handled while waiting for I/O? |
| 117 |
--------------------------------------------------- |
| 118 |
|
| 119 |
On platforms other than Windows, yes, and you don't even |
| 120 |
need threads! But you'll have to restructure your I/O |
| 121 |
code a bit. Tk has the equivalent of Xt's :c:func:`XtAddInput()` call, which allows you |
| 122 |
to register a callback function which will be called from the Tk mainloop when |
| 123 |
I/O is possible on a file descriptor. See :ref:`tkinter-file-handlers`. |
| 124 |
|
| 125 |
|
| 126 |
I can't get key bindings to work in Tkinter: why? |
| 127 |
------------------------------------------------- |
| 128 |
|
| 129 |
An often-heard complaint is that event handlers bound to events with the |
| 130 |
:meth:`bind` method don't get handled even when the appropriate key is pressed. |
| 131 |
|
| 132 |
The most common cause is that the widget to which the binding applies doesn't |
| 133 |
have "keyboard focus". Check out the Tk documentation for the focus command. |
| 134 |
Usually a widget is given the keyboard focus by clicking in it (but not for |
| 135 |
labels; see the takefocus option). |
| 136 |
|
| 137 |
|
| 138 |
|