Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 1 | :tocdepth: 2 |
| 2 | |
| 3 | ========================== |
| 4 | Graphic User Interface FAQ |
| 5 | ========================== |
| 6 | |
| 7 | .. contents:: |
| 8 | |
Georg Brandl | 107690c | 2010-10-06 07:12:17 +0000 | [diff] [blame] | 9 | .. XXX need review for Python 3. |
| 10 | |
| 11 | |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 12 | General GUI Questions |
| 13 | ===================== |
| 14 | |
| 15 | What platform-independent GUI toolkits exist for Python? |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +0000 | [diff] [blame] | 16 | ======================================================== |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +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 |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +0000 | [diff] [blame] | 23 | ------- |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +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 | |
| 31 | wxWindows |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +0000 | [diff] [blame] | 32 | --------- |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 33 | |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +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 | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 39 | |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +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. |
| 45 | |
| 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. |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 49 | |
| 50 | Qt |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +0000 | [diff] [blame] | 51 | --- |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 52 | |
| 53 | There are bindings available for the Qt toolkit (`PyQt |
Georg Brandl | 628e6f9 | 2009-10-27 20:24:45 +0000 | [diff] [blame] | 54 | <http://www.riverbankcomputing.co.uk/software/pyqt/>`_) and for KDE (PyKDE). If |
| 55 | you're writing open source software, you don't need to pay for PyQt, but if you |
| 56 | want to write proprietary applications, you must buy a PyQt license from |
| 57 | `Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4; |
| 58 | Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech |
| 59 | <http://www.trolltech.com>`_. |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 60 | |
| 61 | Gtk+ |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +0000 | [diff] [blame] | 62 | ---- |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 63 | |
| 64 | PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been |
| 65 | implemented by by James Henstridge; see ftp://ftp.gtk.org/pub/gtk/python/. |
| 66 | |
| 67 | FLTK |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +0000 | [diff] [blame] | 68 | ---- |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 69 | |
| 70 | Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet |
| 71 | powerful and mature cross-platform windowing system, are available from `the |
| 72 | PyFLTK project <http://pyfltk.sourceforge.net>`_. |
| 73 | |
| 74 | |
| 75 | FOX |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +0000 | [diff] [blame] | 76 | ---- |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 77 | |
| 78 | A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy |
| 79 | <http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants |
| 80 | and Windows. |
| 81 | |
| 82 | |
| 83 | OpenGL |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +0000 | [diff] [blame] | 84 | ------ |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 85 | |
| 86 | For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_. |
| 87 | |
| 88 | |
| 89 | What platform-specific GUI toolkits exist for Python? |
Georg Brandl | c62efa8 | 2010-07-11 10:41:07 +0000 | [diff] [blame] | 90 | ======================================================== |
Georg Brandl | cb7cb24 | 2009-10-27 20:20:38 +0000 | [diff] [blame] | 91 | |
| 92 | `The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and |
| 93 | ever-growing set of modules that support the native Mac toolbox calls. The port |
| 94 | includes support for MacOS9 and MacOS X's Carbon libraries. By installing the |
| 95 | `PyObjc Objective-C bridge <http://pyobjc.sourceforge.net>`_, Python programs |
| 96 | can use MacOS X's Cocoa libraries. See the documentation that comes with the Mac |
| 97 | port. |
| 98 | |
| 99 | :ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the |
| 100 | Microsoft Foundation Classes and a Python programming environment using it |
| 101 | that's written mostly in Python. |
| 102 | |
| 103 | |
| 104 | Tkinter questions |
| 105 | ================= |
| 106 | |
| 107 | How do I freeze Tkinter applications? |
| 108 | ------------------------------------- |
| 109 | |
| 110 | Freeze is a tool to create stand-alone applications. When freezing Tkinter |
| 111 | applications, the applications will not be truly stand-alone, as the application |
| 112 | will still need the Tcl and Tk libraries. |
| 113 | |
| 114 | One solution is to ship the application with the tcl and tk libraries, and point |
| 115 | to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY` |
| 116 | environment variables. |
| 117 | |
| 118 | To get truly stand-alone applications, the Tcl scripts that form the library |
| 119 | have to be integrated into the application as well. One tool supporting that is |
| 120 | SAM (stand-alone modules), which is part of the Tix distribution |
| 121 | (http://tix.mne.com). Build Tix with SAM enabled, perform the appropriate call |
| 122 | to Tclsam_init etc inside Python's Modules/tkappinit.c, and link with libtclsam |
| 123 | and libtksam (you might include the Tix libraries as well). |
| 124 | |
| 125 | |
| 126 | Can I have Tk events handled while waiting for I/O? |
| 127 | --------------------------------------------------- |
| 128 | |
| 129 | Yes, and you don't even need threads! But you'll have to restructure your I/O |
| 130 | code a bit. Tk has the equivalent of Xt's XtAddInput() call, which allows you |
| 131 | to register a callback function which will be called from the Tk mainloop when |
| 132 | I/O is possible on a file descriptor. Here's what you need:: |
| 133 | |
| 134 | from Tkinter import tkinter |
| 135 | tkinter.createfilehandler(file, mask, callback) |
| 136 | |
| 137 | The file may be a Python file or socket object (actually, anything with a |
| 138 | fileno() method), or an integer file descriptor. The mask is one of the |
| 139 | constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as |
| 140 | follows:: |
| 141 | |
| 142 | callback(file, mask) |
| 143 | |
| 144 | You must unregister the callback when you're done, using :: |
| 145 | |
| 146 | tkinter.deletefilehandler(file) |
| 147 | |
| 148 | Note: since you don't know *how many bytes* are available for reading, you can't |
| 149 | use the Python file object's read or readline methods, since these will insist |
| 150 | on reading a predefined number of bytes. For sockets, the :meth:`recv` or |
| 151 | :meth:`recvfrom` methods will work fine; for other files, use |
| 152 | ``os.read(file.fileno(), maxbytecount)``. |
| 153 | |
| 154 | |
| 155 | I can't get key bindings to work in Tkinter: why? |
| 156 | ------------------------------------------------- |
| 157 | |
| 158 | An often-heard complaint is that event handlers bound to events with the |
| 159 | :meth:`bind` method don't get handled even when the appropriate key is pressed. |
| 160 | |
| 161 | The most common cause is that the widget to which the binding applies doesn't |
| 162 | have "keyboard focus". Check out the Tk documentation for the focus command. |
| 163 | Usually a widget is given the keyboard focus by clicking in it (but not for |
| 164 | labels; see the takefocus option). |