blob: 477d8c633438d1a92371fdbf99dbf33d897d3a75 [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
Georg Brandl5d941342016-02-26 19:37:12 +010032`binary distributions <https://www.python.org/downloads/>`_ of Python) and use.
Antoine Pitroueb50ba52011-02-05 10:43:57 +000033For more info about Tk, including pointers to the source, see the
Serhiy Storchaka6dff0202016-05-07 10:49:07 +030034`Tcl/Tk home page <https://www.tcl.tk>`_. Tcl/Tk is fully portable to the
Ned Deilyecd9e8a2014-04-12 09:32:04 -070035Mac OS X, 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
Serhiy Storchaka6dff0202016-05-07 10:49:07 +030040wxWidgets (https://www.wxwidgets.org) is a free, portable GUI class
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000041library written in C++ that provides a native look and feel on a
Ned Deilyecd9e8a2014-04-12 09:32:04 -070042number of platforms, with Windows, Mac OS X, GTK, X11, all listed as
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000043current 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
Georg Brandl5d941342016-02-26 19:37:12 +010061<https://riverbankcomputing.com/software/pyqt/intro>`_ or `PySide
Serhiy Storchaka90be7332016-04-11 12:18:56 +030062<https://wiki.qt.io/PySide>`_) and for KDE (`PyKDE4 <https://techbase.kde.org/Languages/Python/Using_PyKDE_4>`__).
Antoine Pitroueb50ba52011-02-05 10:43:57 +000063PyQt is currently more mature than PySide, but you must buy a PyQt license from
Georg Brandl5d941342016-02-26 19:37:12 +010064`Riverbank Computing <https://www.riverbankcomputing.com/commercial/license-faq>`_
Antoine Pitroueb50ba52011-02-05 10:43:57 +000065if 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
Georg Brandl5d941342016-02-26 19:37:12 +010068are available from `The Qt Company <https://www.qt.io/licensing/>`_.
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
Georg Brandl5d941342016-02-26 19:37:12 +010073The `GObject introspection bindings <https://wiki.gnome.org/Projects/PyGObject>`_
Antoine Pitrou7462e872011-12-03 20:59:24 +010074for Python allow you to write GTK+ 3 applications. There is also a
Serhiy Storchaka6dff0202016-05-07 10:49:07 +030075`Python GTK+ 3 Tutorial <https://python-gtk-3-tutorial.readthedocs.org/en/latest/>`_.
Antoine Pitrou7462e872011-12-03 20:59:24 +010076
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
Berker Peksagd2d673f2017-01-02 05:44:49 +030080Kivy
81----
82
83`Kivy <https://kivy.org/>`_ is a cross-platform GUI library supporting both
84desktop operating systems (Windows, macOS, Linux) and mobile devices (Android,
85iOS). It is written in Python and Cython, and can use a range of windowing
86backends.
87
88Kivy is free and open source software distributed under the MIT license.
89
Georg Brandld7413152009-10-11 21:25:26 +000090FLTK
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000091----
Georg Brandld7413152009-10-11 21:25:26 +000092
93Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
94powerful and mature cross-platform windowing system, are available from `the
95PyFLTK project <http://pyfltk.sourceforge.net>`_.
96
97
98FOX
Benjamin Petersond7c3ed52010-06-27 22:32:30 +000099----
Georg Brandld7413152009-10-11 21:25:26 +0000100
101A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy
102<http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants
103and Windows.
104
105
106OpenGL
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000107------
Georg Brandld7413152009-10-11 21:25:26 +0000108
109For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
110
111
112What platform-specific GUI toolkits exist for Python?
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000113========================================================
Georg Brandld7413152009-10-11 21:25:26 +0000114
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000115By installing the `PyObjc Objective-C bridge
Georg Brandl77fe77d2014-10-29 09:24:54 +0100116<https://pythonhosted.org/pyobjc/>`_, Python programs can use Mac OS X's
Ned Deilyecd9e8a2014-04-12 09:32:04 -0700117Cocoa libraries.
Georg Brandld7413152009-10-11 21:25:26 +0000118
119:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000120Microsoft Foundation Classes and a Python programming environment
121that's written mostly in Python using the MFC classes.
Georg Brandld7413152009-10-11 21:25:26 +0000122
123
124Tkinter questions
125=================
126
127How do I freeze Tkinter applications?
128-------------------------------------
129
130Freeze is a tool to create stand-alone applications. When freezing Tkinter
131applications, the applications will not be truly stand-alone, as the application
132will still need the Tcl and Tk libraries.
133
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000134One solution is to ship the application with the Tcl and Tk libraries, and point
Georg Brandld7413152009-10-11 21:25:26 +0000135to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
136environment variables.
137
138To get truly stand-alone applications, the Tcl scripts that form the library
139have to be integrated into the application as well. One tool supporting that is
140SAM (stand-alone modules), which is part of the Tix distribution
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000141(http://tix.sourceforge.net/).
142
143Build Tix with SAM enabled, perform the appropriate call to
Georg Brandl60203b42010-10-06 10:11:56 +0000144:c:func:`Tclsam_init`, etc. inside Python's
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000145:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you
146might include the Tix libraries as well).
Georg Brandld7413152009-10-11 21:25:26 +0000147
148
149Can I have Tk events handled while waiting for I/O?
150---------------------------------------------------
151
Terry Jan Reedyd9865632015-05-17 14:49:26 -0400152On platforms other than Windows, yes, and you don't even
153need threads! But you'll have to restructure your I/O
Georg Brandl60203b42010-10-06 10:11:56 +0000154code a bit. Tk has the equivalent of Xt's :c:func:`XtAddInput()` call, which allows you
Georg Brandld7413152009-10-11 21:25:26 +0000155to register a callback function which will be called from the Tk mainloop when
Terry Jan Reedyd9865632015-05-17 14:49:26 -0400156I/O is possible on a file descriptor. See :ref:`tkinter-file-handlers`.
Georg Brandld7413152009-10-11 21:25:26 +0000157
158
159I can't get key bindings to work in Tkinter: why?
160-------------------------------------------------
161
162An often-heard complaint is that event handlers bound to events with the
163:meth:`bind` method don't get handled even when the appropriate key is pressed.
164
165The most common cause is that the widget to which the binding applies doesn't
166have "keyboard focus". Check out the Tk documentation for the focus command.
167Usually a widget is given the keyboard focus by clicking in it (but not for
168labels; see the takefocus option).