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 | |
| 28 | wxWindows |
| 29 | ''''''''' |
| 30 | |
| 31 | wxWindows is a portable GUI class library written in C++ that's a portable |
| 32 | interface to various platform-specific libraries; wxWidgets is a Python |
| 33 | interface to wxWindows. wxWindows supports Windows and MacOS; on Unix variants, |
| 34 | it supports both GTk+ and Motif toolkits. wxWindows preserves the look and feel |
| 35 | of the underlying graphics toolkit, and there is quite a rich widget set and |
| 36 | collection of GDI classes. See `the wxWindows page <http://www.wxwindows.org>`_ |
| 37 | for more details. |
| 38 | |
| 39 | `wxWidgets <http://wxwidgets.org>`_ is an extension module that wraps many of |
| 40 | the wxWindows C++ classes, and is quickly gaining popularity amongst Python |
| 41 | developers. You can get wxWidgets as part of the source or CVS distribution of |
| 42 | wxWindows, or directly from its home page. |
| 43 | |
| 44 | Qt |
| 45 | ''' |
| 46 | |
| 47 | There are bindings available for the Qt toolkit (`PyQt |
Georg Brandl | 495f7b5 | 2009-10-27 15:28:25 +0000 | [diff] [blame] | 48 | <http://www.riverbankcomputing.co.uk/software/pyqt/>`_) and for KDE (PyKDE). If |
| 49 | you're writing open source software, you don't need to pay for PyQt, but if you |
| 50 | want to write proprietary applications, you must buy a PyQt license from |
| 51 | `Riverbank Computing <http://www.riverbankcomputing.co.uk>`_ and (up to Qt 4.4; |
| 52 | Qt 4.5 upwards is licensed under the LGPL license) a Qt license from `Trolltech |
| 53 | <http://www.trolltech.com>`_. |
Georg Brandl | d741315 | 2009-10-11 21:25:26 +0000 | [diff] [blame] | 54 | |
| 55 | Gtk+ |
| 56 | '''' |
| 57 | |
| 58 | PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been |
| 59 | implemented by by James Henstridge; see ftp://ftp.gtk.org/pub/gtk/python/. |
| 60 | |
| 61 | FLTK |
| 62 | '''' |
| 63 | |
| 64 | Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet |
| 65 | powerful and mature cross-platform windowing system, are available from `the |
| 66 | PyFLTK project <http://pyfltk.sourceforge.net>`_. |
| 67 | |
| 68 | |
| 69 | FOX |
| 70 | ''' |
| 71 | |
| 72 | A wrapper for `the FOX toolkit <http://www.fox-toolkit.org/>`_ called `FXpy |
| 73 | <http://fxpy.sourceforge.net/>`_ is available. FOX supports both Unix variants |
| 74 | and Windows. |
| 75 | |
| 76 | |
| 77 | OpenGL |
| 78 | '''''' |
| 79 | |
| 80 | For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_. |
| 81 | |
| 82 | |
| 83 | What platform-specific GUI toolkits exist for Python? |
| 84 | ----------------------------------------------------- |
| 85 | |
| 86 | `The Mac port <http://python.org/download/mac>`_ by Jack Jansen has a rich and |
| 87 | ever-growing set of modules that support the native Mac toolbox calls. The port |
| 88 | includes support for MacOS9 and MacOS X's Carbon libraries. By installing the |
| 89 | `PyObjc Objective-C bridge <http://pyobjc.sourceforge.net>`_, Python programs |
| 90 | can use MacOS X's Cocoa libraries. See the documentation that comes with the Mac |
| 91 | port. |
| 92 | |
| 93 | :ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the |
| 94 | Microsoft Foundation Classes and a Python programming environment using it |
| 95 | that's written mostly in Python. |
| 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 | |
| 108 | One solution is to ship the application with the tcl and tk libraries, and point |
| 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 |
| 115 | (http://tix.mne.com). Build Tix with SAM enabled, perform the appropriate call |
| 116 | to Tclsam_init etc inside Python's Modules/tkappinit.c, and link with libtclsam |
| 117 | and libtksam (you might include the Tix libraries as well). |
| 118 | |
| 119 | |
| 120 | Can I have Tk events handled while waiting for I/O? |
| 121 | --------------------------------------------------- |
| 122 | |
| 123 | Yes, and you don't even need threads! But you'll have to restructure your I/O |
| 124 | code a bit. Tk has the equivalent of Xt's XtAddInput() call, which allows you |
| 125 | to register a callback function which will be called from the Tk mainloop when |
| 126 | I/O is possible on a file descriptor. Here's what you need:: |
| 127 | |
| 128 | from Tkinter import tkinter |
| 129 | tkinter.createfilehandler(file, mask, callback) |
| 130 | |
| 131 | The file may be a Python file or socket object (actually, anything with a |
| 132 | fileno() method), or an integer file descriptor. The mask is one of the |
| 133 | constants tkinter.READABLE or tkinter.WRITABLE. The callback is called as |
| 134 | follows:: |
| 135 | |
| 136 | callback(file, mask) |
| 137 | |
| 138 | You must unregister the callback when you're done, using :: |
| 139 | |
| 140 | tkinter.deletefilehandler(file) |
| 141 | |
| 142 | Note: since you don't know *how many bytes* are available for reading, you can't |
| 143 | use the Python file object's read or readline methods, since these will insist |
| 144 | on reading a predefined number of bytes. For sockets, the :meth:`recv` or |
| 145 | :meth:`recvfrom` methods will work fine; for other files, use |
| 146 | ``os.read(file.fileno(), maxbytecount)``. |
| 147 | |
| 148 | |
| 149 | I can't get key bindings to work in Tkinter: why? |
| 150 | ------------------------------------------------- |
| 151 | |
| 152 | An often-heard complaint is that event handlers bound to events with the |
| 153 | :meth:`bind` method don't get handled even when the appropriate key is pressed. |
| 154 | |
| 155 | The most common cause is that the widget to which the binding applies doesn't |
| 156 | have "keyboard focus". Check out the Tk documentation for the focus command. |
| 157 | Usually a widget is given the keyboard focus by clicking in it (but not for |
| 158 | labels; see the takefocus option). |
| 159 | |
| 160 | |
| 161 | |