| 1 |
:tocdepth: 2 |
| 2 |
|
| 3 |
========================== |
| 4 |
Graphic User Interface FAQ |
| 5 |
========================== |
| 6 |
|
| 7 |
.. only:: html |
| 8 |
|
| 9 |
.. contents:: |
| 10 |
|
| 11 |
.. XXX need review for Python 3. |
| 12 |
|
| 13 |
|
| 14 |
General GUI Questions |
| 15 |
===================== |
| 16 |
|
| 17 |
What GUI toolkits exist for Python? |
| 18 |
=================================== |
| 19 |
|
| 20 |
Standard builds of Python include an object-oriented interface to the Tcl/Tk |
| 21 |
widget set, called :ref:`tkinter <Tkinter>`. This is probably the easiest to |
| 22 |
install (since it comes included with most |
| 23 |
`binary distributions <https://www.python.org/downloads/>`_ of Python) and use. |
| 24 |
For more info about Tk, including pointers to the source, see the |
| 25 |
`Tcl/Tk home page <https://www.tcl.tk>`_. Tcl/Tk is fully portable to the |
| 26 |
macOS, Windows, and Unix platforms. |
| 27 |
|
| 28 |
Depending on what platform(s) you are aiming at, there are also several |
| 29 |
alternatives. A `list of cross-platform |
| 30 |
<https://wiki.python.org/moin/GuiProgramming#Cross-Platform_Frameworks>`_ and |
| 31 |
`platform-specific |
| 32 |
<https://wiki.python.org/moin/GuiProgramming#Platform-specific_Frameworks>`_ GUI |
| 33 |
frameworks can be found on the python wiki. |
| 34 |
|
| 35 |
Tkinter questions |
| 36 |
================= |
| 37 |
|
| 38 |
How do I freeze Tkinter applications? |
| 39 |
------------------------------------- |
| 40 |
|
| 41 |
Freeze is a tool to create stand-alone applications. When freezing Tkinter |
| 42 |
applications, the applications will not be truly stand-alone, as the application |
| 43 |
will still need the Tcl and Tk libraries. |
| 44 |
|
| 45 |
One solution is to ship the application with the Tcl and Tk libraries, and point |
| 46 |
to them at run-time using the :envvar:`!TCL_LIBRARY` and :envvar:`!TK_LIBRARY` |
| 47 |
environment variables. |
| 48 |
|
| 49 |
Various third-party freeze libraries such as py2exe and cx_Freeze have |
| 50 |
handling for Tkinter applications built-in. |
| 51 |
|
| 52 |
|
| 53 |
Can I have Tk events handled while waiting for I/O? |
| 54 |
--------------------------------------------------- |
| 55 |
|
| 56 |
On platforms other than Windows, yes, and you don't even |
| 57 |
need threads! But you'll have to restructure your I/O |
| 58 |
code a bit. Tk has the equivalent of Xt's :c:func:`!XtAddInput` call, which allows you |
| 59 |
to register a callback function which will be called from the Tk mainloop when |
| 60 |
I/O is possible on a file descriptor. See :ref:`tkinter-file-handlers`. |
| 61 |
|
| 62 |
|
| 63 |
I can't get key bindings to work in Tkinter: why? |
| 64 |
------------------------------------------------- |
| 65 |
|
| 66 |
An often-heard complaint is that event handlers :ref:`bound <bindings-and-events>` |
| 67 |
to events with the :meth:`!bind` method |
| 68 |
don't get handled even when the appropriate key is pressed. |
| 69 |
|
| 70 |
The most common cause is that the widget to which the binding applies doesn't |
| 71 |
have "keyboard focus". Check out the Tk documentation for the focus command. |
| 72 |
Usually a widget is given the keyboard focus by clicking in it (but not for |
| 73 |
labels; see the takefocus option). |