3 ==========================
4 Graphic User Interface FAQ
5 ==========================
12 What platform-independent GUI toolkits exist for Python?
13 --------------------------------------------------------
15 Depending on what platform(s) you are aiming at, there are several.
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
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>`__.
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.
40 wxWidgets supports Windows and MacOS; on Unix variants,
41 it supports both GTk+ and Motif toolkits.
46 There are bindings available for the Qt toolkit (`PyQt
47 <http://www.riverbankcomputing.co.uk/software/pyqt/>`_) and for KDE (`PyKDE <http://www.riverbankcomputing.co.uk/software/pykde/intro>`__). If
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>`_.
57 PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
58 implemented by James Henstridge; see <http://www.pygtk.org>.
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>`_.
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
79 For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
82 What platform-specific GUI toolkits exist for Python?
83 -----------------------------------------------------
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
87 supports MacOS X's Carbon libraries.
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.
93 :ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
94 Microsoft Foundation Classes and a Python programming environment
95 that's written mostly in Python using the MFC classes.
101 How do I freeze Tkinter applications?
102 -------------------------------------
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.
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.
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.sourceforge.net/).
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).
123 Can I have Tk events handled while waiting for I/O?
124 ---------------------------------------------------
126 Yes, and you don't even need threads! But you'll have to restructure your I/O
127 code a bit. Tk has the equivalent of Xt's :cfunc:`XtAddInput()` call, which allows you
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::
131 from Tkinter import tkinter
132 tkinter.createfilehandler(file, mask, callback)
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
141 You must unregister the callback when you're done, using ::
143 tkinter.deletefilehandler(file)
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)``.
152 I can't get key bindings to work in Tkinter: why?
153 -------------------------------------------------
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.
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).