Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 1 | :tocdepth: 2 |
| 2 | |
| 3 | ========================== |
| 4 | Graphic User Interface FAQ |
| 5 | ========================== |
| 6 | |
| 7 | .. contents:: |
| 8 | |
Georg Brandl | 62423cb | 2009-12-19 17:59:59 +0000 | [diff] [blame] | 9 | .. XXX need review for Python 3. |
| 10 | |
| 11 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 12 | General GUI Questions |
| 13 | ===================== |
| 14 | |
| 15 | What platform-independent GUI toolkits exist for Python? |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 16 | ======================================================== |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 17 | |
Antoine Pitrou | eb50ba5 | 2011-02-05 10:43:57 +0000 | [diff] [blame^] | 18 | Depending on what platform(s) you are aiming at, there are several. Some |
| 19 | of them haven't been ported to Python 3 yet. At least `Tkinter`_ and `Qt`_ |
| 20 | are known to be Python 3-compatible. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 21 | |
| 22 | .. XXX check links |
| 23 | |
| 24 | Tkinter |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 25 | ------- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 26 | |
| 27 | 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^] | 28 | widget set, called :ref:`tkinter <Tkinter>`. This is probably the easiest to |
| 29 | install (since it comes included with most |
| 30 | `binary distributions <http://www.python.org/download/>`_ of Python) and use. |
| 31 | For more info about Tk, including pointers to the source, see the |
| 32 | `Tcl/Tk home page <http://www.tcl.tk>`_. Tcl/Tk is fully portable to the |
| 33 | MacOS, Windows, and Unix platforms. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 34 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 35 | wxWidgets |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 36 | --------- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 37 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 38 | wxWidgets (http://www.wxwidgets.org) is a free, portable GUI class |
| 39 | library written in C++ that provides a native look and feel on a |
| 40 | number of platforms, with Windows, MacOS X, GTK, X11, all listed as |
| 41 | current stable targets. Language bindings are available for a number |
| 42 | of languages including Python, Perl, Ruby, etc. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 43 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 44 | wxPython (http://www.wxpython.org) is the Python binding for |
| 45 | wxwidgets. While it often lags slightly behind the official wxWidgets |
| 46 | releases, it also offers a number of features via pure Python |
| 47 | extensions that are not available in other language bindings. There |
| 48 | is an active wxPython user and developer community. |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 49 | |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 50 | Both wxWidgets and wxPython are free, open source, software with |
| 51 | permissive licences that allow their use in commercial products as |
| 52 | well as in freeware or shareware. |
| 53 | |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 54 | |
| 55 | Qt |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 56 | --- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 57 | |
Antoine Pitrou | eb50ba5 | 2011-02-05 10:43:57 +0000 | [diff] [blame^] | 58 | There are bindings available for the Qt toolkit (using either `PyQt |
| 59 | <http://www.riverbankcomputing.co.uk/software/pyqt/>`_ or `PySide |
| 60 | <http://www.pyside.org/>`_) and for KDE (`PyKDE <http://www.riverbankcomputing.co.uk/software/pykde/intro>`__). |
| 61 | PyQt is currently more mature than PySide, but you must buy a PyQt license from |
| 62 | `Riverbank Computing <http://www.riverbankcomputing.co.uk/software/pyqt/license>`_ |
| 63 | if you want to write proprietary applications. PySide is free for all applications. |
| 64 | |
| 65 | Qt 4.5 upwards is licensed under the LGPL license; also, commercial licenses |
| 66 | are available from `Nokia <http://qt.nokia.com/>`_. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 67 | |
| 68 | Gtk+ |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 69 | ---- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 70 | |
| 71 | PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 72 | implemented by James Henstridge; see <http://www.pygtk.org>. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 73 | |
| 74 | FLTK |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 75 | ---- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 76 | |
| 77 | Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet |
| 78 | powerful and mature cross-platform windowing system, are available from `the |
| 79 | PyFLTK project <http://pyfltk.sourceforge.net>`_. |
| 80 | |
| 81 | |
| 82 | FOX |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 83 | ---- |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 84 | |
| 85 | A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy |
| 86 | <http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants |
| 87 | and Windows. |
| 88 | |
| 89 | |
| 90 | OpenGL |
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 | For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_. |
| 94 | |
| 95 | |
| 96 | What platform-specific GUI toolkits exist for Python? |
Benjamin Peterson | d7c3ed5 | 2010-06-27 22:32:30 +0000 | [diff] [blame] | 97 | ======================================================== |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 98 | |
| 99 | `The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and |
| 100 | ever-growing set of modules that support the native Mac toolbox calls. The port |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 101 | supports MacOS X's Carbon libraries. |
| 102 | |
| 103 | By installing the `PyObjc Objective-C bridge |
| 104 | <http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's |
| 105 | Cocoa libraries. See the documentation that comes with the Mac port. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 106 | |
| 107 | :ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 108 | Microsoft Foundation Classes and a Python programming environment |
| 109 | that's written mostly in Python using the MFC classes. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | Tkinter questions |
| 113 | ================= |
| 114 | |
| 115 | How do I freeze Tkinter applications? |
| 116 | ------------------------------------- |
| 117 | |
| 118 | Freeze is a tool to create stand-alone applications. When freezing Tkinter |
| 119 | applications, the applications will not be truly stand-alone, as the application |
| 120 | will still need the Tcl and Tk libraries. |
| 121 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 122 | 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] | 123 | to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY` |
| 124 | environment variables. |
| 125 | |
| 126 | To get truly stand-alone applications, the Tcl scripts that form the library |
| 127 | have to be integrated into the application as well. One tool supporting that is |
| 128 | SAM (stand-alone modules), which is part of the Tix distribution |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 129 | (http://tix.sourceforge.net/). |
| 130 | |
| 131 | Build Tix with SAM enabled, perform the appropriate call to |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 132 | :c:func:`Tclsam_init`, etc. inside Python's |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 133 | :file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you |
| 134 | might include the Tix libraries as well). |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 135 | |
| 136 | |
| 137 | Can I have Tk events handled while waiting for I/O? |
| 138 | --------------------------------------------------- |
| 139 | |
| 140 | Yes, and you don't even need threads! But you'll have to restructure your I/O |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 141 | 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] | 142 | to register a callback function which will be called from the Tk mainloop when |
| 143 | I/O is possible on a file descriptor. Here's what you need:: |
| 144 | |
| 145 | from Tkinter import tkinter |
| 146 | tkinter.createfilehandler(file, mask, callback) |
| 147 | |
| 148 | The file may be a Python file or socket object (actually, anything with a |
| 149 | fileno() method), or an integer file descriptor. The mask is one of the |
| 150 | constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as |
| 151 | follows:: |
| 152 | |
| 153 | callback(file, mask) |
| 154 | |
| 155 | You must unregister the callback when you're done, using :: |
| 156 | |
| 157 | tkinter.deletefilehandler(file) |
| 158 | |
| 159 | Note: since you don't know *how many bytes* are available for reading, you can't |
| 160 | use the Python file object's read or readline methods, since these will insist |
| 161 | on reading a predefined number of bytes. For sockets, the :meth:`recv` or |
| 162 | :meth:`recvfrom` methods will work fine; for other files, use |
| 163 | ``os.read(file.fileno(), maxbytecount)``. |
| 164 | |
| 165 | |
| 166 | I can't get key bindings to work in Tkinter: why? |
| 167 | ------------------------------------------------- |
| 168 | |
| 169 | An often-heard complaint is that event handlers bound to events with the |
| 170 | :meth:`bind` method don't get handled even when the appropriate key is pressed. |
| 171 | |
| 172 | The most common cause is that the widget to which the binding applies doesn't |
| 173 | have "keyboard focus". Check out the Tk documentation for the focus command. |
| 174 | Usually a widget is given the keyboard focus by clicking in it (but not for |
| 175 | labels; see the takefocus option). |