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