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