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