Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 1 | :tocdepth: 2 |
| 2 | |
| 3 | ========================== |
| 4 | Graphic User Interface FAQ |
| 5 | ========================== |
| 6 | |
Georg Brandl | 44ea77b | 2013-03-28 13:28:44 +0100 | [diff] [blame] | 7 | .. only:: html |
| 8 | |
| 9 | .. contents:: |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 10 | |
Georg Brandl | 62423cb | 2009-12-19 17:59:59 +0000 | [diff] [blame] | 11 | .. XXX need review for Python 3. |
| 12 | |
| 13 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 14 | General GUI Questions |
| 15 | ===================== |
| 16 | |
| 17 | What platform-independent GUI toolkits exist for Python? |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 18 | ======================================================== |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 19 | |
Antoine Pitrou | eb50ba5 | 2011-02-05 10:43:57 +0000 | [diff] [blame] | 20 | Depending on what platform(s) you are aiming at, there are several. Some |
| 21 | of them haven't been ported to Python 3 yet. At least `Tkinter`_ and `Qt`_ |
| 22 | are known to be Python 3-compatible. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 23 | |
| 24 | .. XXX check links |
| 25 | |
| 26 | Tkinter |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 27 | ------- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 28 | |
| 29 | Standard builds of Python include an object-oriented interface to the Tcl/Tk |
Antoine Pitrou | eb50ba5 | 2011-02-05 10:43:57 +0000 | [diff] [blame] | 30 | widget set, called :ref:`tkinter <Tkinter>`. This is probably the easiest to |
| 31 | install (since it comes included with most |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 32 | `binary distributions <https://www.python.org/downloads/>`_ of Python) and use. |
Antoine Pitrou | eb50ba5 | 2011-02-05 10:43:57 +0000 | [diff] [blame] | 33 | For more info about Tk, including pointers to the source, see the |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 34 | `Tcl/Tk home page <https://www.tcl.tk>`_. Tcl/Tk is fully portable to the |
Ned Deily | ecd9e8a | 2014-04-12 09:32:04 -0700 | [diff] [blame] | 35 | Mac OS X, Windows, and Unix platforms. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 36 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 37 | wxWidgets |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 38 | --------- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 39 | |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 40 | wxWidgets (https://www.wxwidgets.org) is a free, portable GUI class |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 41 | library written in C++ that provides a native look and feel on a |
Ned Deily | ecd9e8a | 2014-04-12 09:32:04 -0700 | [diff] [blame] | 42 | number of platforms, with Windows, Mac OS X, GTK, X11, all listed as |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 43 | current stable targets. Language bindings are available for a number |
| 44 | of languages including Python, Perl, Ruby, etc. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 45 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 46 | wxPython (http://www.wxpython.org) is the Python binding for |
| 47 | wxwidgets. While it often lags slightly behind the official wxWidgets |
| 48 | releases, it also offers a number of features via pure Python |
| 49 | extensions that are not available in other language bindings. There |
| 50 | is an active wxPython user and developer community. |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 51 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 52 | Both wxWidgets and wxPython are free, open source, software with |
| 53 | permissive licences that allow their use in commercial products as |
| 54 | well as in freeware or shareware. |
| 55 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 56 | |
| 57 | Qt |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 58 | --- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 59 | |
Antoine Pitrou | eb50ba5 | 2011-02-05 10:43:57 +0000 | [diff] [blame] | 60 | There are bindings available for the Qt toolkit (using either `PyQt |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 61 | <https://riverbankcomputing.com/software/pyqt/intro>`_ or `PySide |
Serhiy Storchaka | 90be733 | 2016-04-11 12:18:56 +0300 | [diff] [blame] | 62 | <https://wiki.qt.io/PySide>`_) and for KDE (`PyKDE4 <https://techbase.kde.org/Languages/Python/Using_PyKDE_4>`__). |
Antoine Pitrou | eb50ba5 | 2011-02-05 10:43:57 +0000 | [diff] [blame] | 63 | PyQt is currently more mature than PySide, but you must buy a PyQt license from |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 64 | `Riverbank Computing <https://www.riverbankcomputing.com/commercial/license-faq>`_ |
Antoine Pitrou | eb50ba5 | 2011-02-05 10:43:57 +0000 | [diff] [blame] | 65 | if you want to write proprietary applications. PySide is free for all applications. |
| 66 | |
| 67 | Qt 4.5 upwards is licensed under the LGPL license; also, commercial licenses |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 68 | are available from `The Qt Company <https://www.qt.io/licensing/>`_. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 69 | |
| 70 | Gtk+ |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 71 | ---- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 72 | |
Georg Brandl | 5d94134 | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 73 | The `GObject introspection bindings <https://wiki.gnome.org/Projects/PyGObject>`_ |
Antoine Pitrou | 7462e87 | 2011-12-03 20:59:24 +0100 | [diff] [blame] | 74 | for Python allow you to write GTK+ 3 applications. There is also a |
Serhiy Storchaka | 6dff020 | 2016-05-07 10:49:07 +0300 | [diff] [blame] | 75 | `Python GTK+ 3 Tutorial <https://python-gtk-3-tutorial.readthedocs.org/en/latest/>`_. |
Antoine Pitrou | 7462e87 | 2011-12-03 20:59:24 +0100 | [diff] [blame] | 76 | |
| 77 | The older PyGtk bindings for the `Gtk+ 2 toolkit <http://www.gtk.org>`_ have |
| 78 | been implemented by James Henstridge; see <http://www.pygtk.org>. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 79 | |
Berker Peksag | d2d673f | 2017-01-02 05:44:49 +0300 | [diff] [blame] | 80 | Kivy |
| 81 | ---- |
| 82 | |
| 83 | `Kivy <https://kivy.org/>`_ is a cross-platform GUI library supporting both |
| 84 | desktop operating systems (Windows, macOS, Linux) and mobile devices (Android, |
| 85 | iOS). It is written in Python and Cython, and can use a range of windowing |
| 86 | backends. |
| 87 | |
| 88 | Kivy is free and open source software distributed under the MIT license. |
| 89 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 90 | FLTK |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 91 | ---- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 92 | |
| 93 | Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet |
| 94 | powerful and mature cross-platform windowing system, are available from `the |
| 95 | PyFLTK project <http://pyfltk.sourceforge.net>`_. |
| 96 | |
| 97 | |
| 98 | FOX |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 99 | ---- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 100 | |
| 101 | A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy |
| 102 | <http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants |
| 103 | and Windows. |
| 104 | |
| 105 | |
| 106 | OpenGL |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 107 | ------ |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 108 | |
| 109 | For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_. |
| 110 | |
| 111 | |
| 112 | What platform-specific GUI toolkits exist for Python? |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 113 | ======================================================== |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 114 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 115 | By installing the `PyObjc Objective-C bridge |
Georg Brandl | 77fe77d | 2014-10-29 09:24:54 +0100 | [diff] [blame] | 116 | <https://pythonhosted.org/pyobjc/>`_, Python programs can use Mac OS X's |
Ned Deily | ecd9e8a | 2014-04-12 09:32:04 -0700 | [diff] [blame] | 117 | Cocoa libraries. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 118 | |
| 119 | :ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 120 | Microsoft Foundation Classes and a Python programming environment |
| 121 | that's written mostly in Python using the MFC classes. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 122 | |
| 123 | |
| 124 | Tkinter questions |
| 125 | ================= |
| 126 | |
| 127 | How do I freeze Tkinter applications? |
| 128 | ------------------------------------- |
| 129 | |
| 130 | Freeze is a tool to create stand-alone applications. When freezing Tkinter |
| 131 | applications, the applications will not be truly stand-alone, as the application |
| 132 | will still need the Tcl and Tk libraries. |
| 133 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 134 | One solution is to ship the application with the Tcl and Tk libraries, and point |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 135 | to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY` |
| 136 | environment variables. |
| 137 | |
| 138 | To get truly stand-alone applications, the Tcl scripts that form the library |
| 139 | have to be integrated into the application as well. One tool supporting that is |
| 140 | SAM (stand-alone modules), which is part of the Tix distribution |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 141 | (http://tix.sourceforge.net/). |
| 142 | |
| 143 | Build Tix with SAM enabled, perform the appropriate call to |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 144 | :c:func:`Tclsam_init`, etc. inside Python's |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 145 | :file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you |
| 146 | might include the Tix libraries as well). |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 147 | |
| 148 | |
| 149 | Can I have Tk events handled while waiting for I/O? |
| 150 | --------------------------------------------------- |
| 151 | |
Terry Jan Reedy | d986563 | 2015-05-17 14:49:26 -0400 | [diff] [blame] | 152 | On platforms other than Windows, yes, and you don't even |
| 153 | need threads! But you'll have to restructure your I/O |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 154 | code a bit. Tk has the equivalent of Xt's :c:func:`XtAddInput()` call, which allows you |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 155 | to register a callback function which will be called from the Tk mainloop when |
Terry Jan Reedy | d986563 | 2015-05-17 14:49:26 -0400 | [diff] [blame] | 156 | I/O is possible on a file descriptor. See :ref:`tkinter-file-handlers`. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 157 | |
| 158 | |
| 159 | I can't get key bindings to work in Tkinter: why? |
| 160 | ------------------------------------------------- |
| 161 | |
| 162 | An 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 | |
| 165 | The most common cause is that the widget to which the binding applies doesn't |
| 166 | have "keyboard focus". Check out the Tk documentation for the focus command. |
| 167 | Usually a widget is given the keyboard focus by clicking in it (but not for |
| 168 | labels; see the takefocus option). |