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