Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +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 | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 10 | |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 11 | What platform-independent GUI toolkits exist for Python? |
Andrew M. Kuchling | 3bfd031 | 2010-05-27 13:30:09 +0000 | [diff] [blame] | 12 | ======================================================== |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 13 | |
| 14 | Depending on what platform(s) you are aiming at, there are several. |
| 15 | |
| 16 | .. XXX check links |
| 17 | |
| 18 | Tkinter |
Andrew M. Kuchling | 3bfd031 | 2010-05-27 13:30:09 +0000 | [diff] [blame] | 19 | ------- |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 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 |
Serhiy Storchaka | b4905ef | 2016-05-07 10:50:12 +0300 | [diff] [blame] | 24 | page at https://www.tcl.tk. Tcl/Tk is fully portable to the Mac OS X, Windows, |
Ned Deily | 4624b00 | 2014-04-12 09:31:28 -0700 | [diff] [blame] | 25 | and Unix platforms. |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 26 | |
Andrew M. Kuchling | 5c4e6eb | 2009-10-13 16:11:49 +0000 | [diff] [blame] | 27 | wxWidgets |
Andrew M. Kuchling | 3bfd031 | 2010-05-27 13:30:09 +0000 | [diff] [blame] | 28 | --------- |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 29 | |
Serhiy Storchaka | b4905ef | 2016-05-07 10:50:12 +0300 | [diff] [blame] | 30 | wxWidgets (https://www.wxwidgets.org) is a free, portable GUI class |
Andrew M. Kuchling | 2eaa542 | 2010-05-27 13:22:53 +0000 | [diff] [blame] | 31 | library written in C++ that provides a native look and feel on a |
Ned Deily | 4624b00 | 2014-04-12 09:31:28 -0700 | [diff] [blame] | 32 | number of platforms, with Windows, Mac OS X, GTK, X11, all listed as |
Andrew M. Kuchling | 2eaa542 | 2010-05-27 13:22:53 +0000 | [diff] [blame] | 33 | current stable targets. Language bindings are available for a number |
| 34 | of languages including Python, Perl, Ruby, etc. |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 35 | |
Andrew M. Kuchling | 2eaa542 | 2010-05-27 13:22:53 +0000 | [diff] [blame] | 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. |
Andrew M. Kuchling | 5c4e6eb | 2009-10-13 16:11:49 +0000 | [diff] [blame] | 41 | |
Andrew M. Kuchling | 2eaa542 | 2010-05-27 13:22:53 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 46 | |
| 47 | Qt |
Andrew M. Kuchling | 3bfd031 | 2010-05-27 13:30:09 +0000 | [diff] [blame] | 48 | --- |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 49 | |
Ezio Melotti | 8f89928 | 2013-10-06 01:02:09 +0300 | [diff] [blame] | 50 | There are bindings available for the Qt toolkit (using either `PyQt |
Georg Brandl | 6e0b44e | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 51 | <https://riverbankcomputing.com/software/pyqt/intro>`_ or `PySide |
Serhiy Storchaka | fdcb535 | 2016-04-11 12:18:56 +0300 | [diff] [blame] | 52 | <https://wiki.qt.io/PySide>`_) and for KDE (`PyKDE4 <https://techbase.kde.org/Languages/Python/Using_PyKDE_4>`__). |
Ezio Melotti | 8f89928 | 2013-10-06 01:02:09 +0300 | [diff] [blame] | 53 | PyQt is currently more mature than PySide, but you must buy a PyQt license from |
Georg Brandl | 6e0b44e | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 54 | `Riverbank Computing <https://www.riverbankcomputing.com/commercial/license-faq>`_ |
Ezio Melotti | 8f89928 | 2013-10-06 01:02:09 +0300 | [diff] [blame] | 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 |
Georg Brandl | 6e0b44e | 2016-02-26 19:37:12 +0100 | [diff] [blame] | 58 | are available from `The Qt Company <https://www.qt.io/licensing/>`_. |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 59 | |
| 60 | Gtk+ |
Andrew M. Kuchling | 3bfd031 | 2010-05-27 13:30:09 +0000 | [diff] [blame] | 61 | ---- |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 62 | |
| 63 | PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been |
Andrew M. Kuchling | 5c4e6eb | 2009-10-13 16:11:49 +0000 | [diff] [blame] | 64 | implemented by James Henstridge; see <http://www.pygtk.org>. |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 65 | |
| 66 | FLTK |
Andrew M. Kuchling | 3bfd031 | 2010-05-27 13:30:09 +0000 | [diff] [blame] | 67 | ---- |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 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 | |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 73 | OpenGL |
Andrew M. Kuchling | 3bfd031 | 2010-05-27 13:30:09 +0000 | [diff] [blame] | 74 | ------ |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 75 | |
| 76 | For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_. |
| 77 | |
| 78 | |
| 79 | What platform-specific GUI toolkits exist for Python? |
Andrew M. Kuchling | 3bfd031 | 2010-05-27 13:30:09 +0000 | [diff] [blame] | 80 | ======================================================== |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 81 | |
Andrew M. Kuchling | 5c4e6eb | 2009-10-13 16:11:49 +0000 | [diff] [blame] | 82 | By installing the `PyObjc Objective-C bridge |
Georg Brandl | fa55a31 | 2014-10-29 09:24:54 +0100 | [diff] [blame] | 83 | <https://pythonhosted.org/pyobjc/>`_, Python programs can use Mac OS X's |
Ned Deily | 4624b00 | 2014-04-12 09:31:28 -0700 | [diff] [blame] | 84 | Cocoa libraries. |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 85 | |
| 86 | :ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the |
Andrew M. Kuchling | 5c4e6eb | 2009-10-13 16:11:49 +0000 | [diff] [blame] | 87 | Microsoft Foundation Classes and a Python programming environment |
| 88 | that's written mostly in Python using the MFC classes. |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 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 | |
Andrew M. Kuchling | 5c4e6eb | 2009-10-13 16:11:49 +0000 | [diff] [blame] | 101 | One solution is to ship the application with the Tcl and Tk libraries, and point |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 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 |
Andrew M. Kuchling | 5c4e6eb | 2009-10-13 16:11:49 +0000 | [diff] [blame] | 108 | (http://tix.sourceforge.net/). |
| 109 | |
| 110 | Build Tix with SAM enabled, perform the appropriate call to |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 111 | :c:func:`Tclsam_init`, etc. inside Python's |
Andrew M. Kuchling | 5c4e6eb | 2009-10-13 16:11:49 +0000 | [diff] [blame] | 112 | :file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you |
| 113 | might include the Tix libraries as well). |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | Can I have Tk events handled while waiting for I/O? |
| 117 | --------------------------------------------------- |
| 118 | |
Terry Jan Reedy | 84924e6 | 2015-05-17 14:49:20 -0400 | [diff] [blame] | 119 | On platforms other than Windows, yes, and you don't even |
| 120 | need threads! But you'll have to restructure your I/O |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 121 | code a bit. Tk has the equivalent of Xt's :c:func:`XtAddInput()` call, which allows you |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 122 | to register a callback function which will be called from the Tk mainloop when |
Terry Jan Reedy | 84924e6 | 2015-05-17 14:49:20 -0400 | [diff] [blame] | 123 | I/O is possible on a file descriptor. See :ref:`tkinter-file-handlers`. |
Georg Brandl | 6728c5a | 2009-10-11 18:31:23 +0000 | [diff] [blame] | 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 | |