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 | |
| 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 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame^] | 28 | wxWidgets |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 29 | ''''''''' |
| 30 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame^] | 31 | wxWidgets is a GUI class library written in C++ that's a portable |
| 32 | interface to various platform-specific libraries, and that has a |
| 33 | Python interface called `wxPython <http://www.wxpython.org>`__. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 34 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame^] | 35 | wxWidgets preserves the look and feel of the |
| 36 | underlying graphics toolkit, and has a large set of widgets and |
| 37 | collection of GDI classes. See `the wxWidgets page |
| 38 | <http://www.wxwidgets.org>`_ for more details. |
| 39 | |
| 40 | wxWidgets supports Windows and MacOS; on Unix variants, |
| 41 | it supports both GTk+ and Motif toolkits. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 42 | |
| 43 | Qt |
| 44 | ''' |
| 45 | |
| 46 | There are bindings available for the Qt toolkit (`PyQt |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame^] | 47 | <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] | 48 | you're writing open source software, you don't need to pay for PyQt, but if you |
| 49 | want to write proprietary applications, you must buy a PyQt license from |
| 50 | `Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4; |
| 51 | Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech |
| 52 | <http://www.trolltech.com>`_. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 53 | |
| 54 | Gtk+ |
| 55 | '''' |
| 56 | |
| 57 | PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame^] | 58 | implemented by James Henstridge; see <http://www.pygtk.org>. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 59 | |
| 60 | FLTK |
| 61 | '''' |
| 62 | |
| 63 | Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet |
| 64 | powerful and mature cross-platform windowing system, are available from `the |
| 65 | PyFLTK project <http://pyfltk.sourceforge.net>`_. |
| 66 | |
| 67 | |
| 68 | FOX |
| 69 | ''' |
| 70 | |
| 71 | A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy |
| 72 | <http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants |
| 73 | and Windows. |
| 74 | |
| 75 | |
| 76 | OpenGL |
| 77 | '''''' |
| 78 | |
| 79 | For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_. |
| 80 | |
| 81 | |
| 82 | What platform-specific GUI toolkits exist for Python? |
| 83 | ----------------------------------------------------- |
| 84 | |
| 85 | `The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and |
| 86 | 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^] | 87 | supports MacOS X's Carbon libraries. |
| 88 | |
| 89 | By installing the `PyObjc Objective-C bridge |
| 90 | <http://pyobjc.sourceforge.net>`_, Python programs can use MacOS X's |
| 91 | Cocoa libraries. See the documentation that comes with the Mac port. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 92 | |
| 93 | :ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame^] | 94 | Microsoft Foundation Classes and a Python programming environment |
| 95 | that's written mostly in Python using the MFC classes. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 96 | |
| 97 | |
| 98 | Tkinter questions |
| 99 | ================= |
| 100 | |
| 101 | How do I freeze Tkinter applications? |
| 102 | ------------------------------------- |
| 103 | |
| 104 | Freeze is a tool to create stand-alone applications. When freezing Tkinter |
| 105 | applications, the applications will not be truly stand-alone, as the application |
| 106 | will still need the Tcl and Tk libraries. |
| 107 | |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame^] | 108 | 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] | 109 | to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY` |
| 110 | environment variables. |
| 111 | |
| 112 | To get truly stand-alone applications, the Tcl scripts that form the library |
| 113 | have to be integrated into the application as well. One tool supporting that is |
| 114 | SAM (stand-alone modules), which is part of the Tix distribution |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame^] | 115 | (http://tix.sourceforge.net/). |
| 116 | |
| 117 | Build Tix with SAM enabled, perform the appropriate call to |
| 118 | :cfunc:`Tclsam_init`, etc. inside Python's |
| 119 | :file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you |
| 120 | might include the Tix libraries as well). |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 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 |
Benjamin Peterson | f6489f9 | 2009-11-25 17:46:26 +0000 | [diff] [blame^] | 127 | 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] | 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 | |