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? |
| 16 | -------------------------------------------------------- |
| 17 | |
| 18 | Depending on what platform(s) you are aiming at, there are several. |
| 19 | |
| 20 | .. XXX check links |
| 21 | |
| 22 | Tkinter |
| 23 | ''''''' |
| 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 |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 32 | ''''''''' |
| 33 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 34 | wxWidgets is a GUI class library written in C++ that's a portable |
| 35 | interface to various platform-specific libraries, and that has a |
| 36 | Python interface called `wxPython <http://www.wxpython.org>`__. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 37 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 38 | wxWidgets preserves the look and feel of the |
| 39 | underlying graphics toolkit, and has a large set of widgets and |
| 40 | collection of GDI classes. See `the wxWidgets page |
| 41 | <http://www.wxwidgets.org>`_ for more details. |
| 42 | |
| 43 | wxWidgets supports Windows and MacOS; on Unix variants, |
| 44 | it supports both GTk+ and Motif toolkits. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 45 | |
| 46 | Qt |
| 47 | ''' |
| 48 | |
| 49 | There are bindings available for the Qt toolkit (`PyQt |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 50 | <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] | 51 | you're writing open source software, you don't need to pay for PyQt, but if you |
| 52 | want to write proprietary applications, you must buy a PyQt license from |
| 53 | `Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4; |
| 54 | Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech |
| 55 | <http://www.trolltech.com>`_. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 56 | |
| 57 | Gtk+ |
| 58 | '''' |
| 59 | |
| 60 | PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 61 | implemented by James Henstridge; see <http://www.pygtk.org>. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 62 | |
| 63 | FLTK |
| 64 | '''' |
| 65 | |
| 66 | Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet |
| 67 | powerful and mature cross-platform windowing system, are available from `the |
| 68 | PyFLTK project <http://pyfltk.sourceforge.net>`_. |
| 69 | |
| 70 | |
| 71 | FOX |
| 72 | ''' |
| 73 | |
| 74 | A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy |
| 75 | <http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants |
| 76 | and Windows. |
| 77 | |
| 78 | |
| 79 | OpenGL |
| 80 | '''''' |
| 81 | |
| 82 | For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_. |
| 83 | |
| 84 | |
| 85 | What platform-specific GUI toolkits exist for Python? |
| 86 | ----------------------------------------------------- |
| 87 | |
| 88 | `The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and |
| 89 | 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] | 90 | supports MacOS X's Carbon libraries. |
| 91 | |
| 92 | By installing the `PyObjc Objective-C bridge |
| 93 | <http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's |
| 94 | Cocoa libraries. See the documentation that comes with the Mac port. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 95 | |
| 96 | :ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 97 | Microsoft Foundation Classes and a Python programming environment |
| 98 | that's written mostly in Python using the MFC classes. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 111 | 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] | 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 |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 118 | (http://tix.sourceforge.net/). |
| 119 | |
| 120 | Build Tix with SAM enabled, perform the appropriate call to |
| 121 | :cfunc:`Tclsam_init`, etc. inside Python's |
| 122 | :file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you |
| 123 | might include the Tix libraries as well). |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame] | 130 | 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] | 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). |