blob: 9228fdded9f222fbc8f3004b3751121c2c5bd1ea [file] [log] [blame]
Georg Brandl6728c5a2009-10-11 18:31:23 +00001:tocdepth: 2
2
3==========================
4Graphic User Interface FAQ
5==========================
6
Georg Brandl44ea77b2013-03-28 13:28:44 +01007.. only:: html
8
9 .. contents::
Georg Brandl6728c5a2009-10-11 18:31:23 +000010
Georg Brandl6728c5a2009-10-11 18:31:23 +000011What platform-independent GUI toolkits exist for Python?
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000012========================================================
Georg Brandl6728c5a2009-10-11 18:31:23 +000013
14Depending on what platform(s) you are aiming at, there are several.
15
16.. XXX check links
17
18Tkinter
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000019-------
Georg Brandl6728c5a2009-10-11 18:31:23 +000020
21Standard builds of Python include an object-oriented interface to the Tcl/Tk
22widget set, called Tkinter. This is probably the easiest to install and use.
23For more info about Tk, including pointers to the source, see the Tcl/Tk home
Serhiy Storchakab4905ef2016-05-07 10:50:12 +030024page at https://www.tcl.tk. Tcl/Tk is fully portable to the Mac OS X, Windows,
Ned Deily4624b002014-04-12 09:31:28 -070025and Unix platforms.
Georg Brandl6728c5a2009-10-11 18:31:23 +000026
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000027wxWidgets
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000028---------
Georg Brandl6728c5a2009-10-11 18:31:23 +000029
Serhiy Storchakab4905ef2016-05-07 10:50:12 +030030wxWidgets (https://www.wxwidgets.org) is a free, portable GUI class
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +000031library written in C++ that provides a native look and feel on a
Ned Deily4624b002014-04-12 09:31:28 -070032number of platforms, with Windows, Mac OS X, GTK, X11, all listed as
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +000033current stable targets. Language bindings are available for a number
34of languages including Python, Perl, Ruby, etc.
Georg Brandl6728c5a2009-10-11 18:31:23 +000035
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +000036wxPython (http://www.wxpython.org) is the Python binding for
37wxwidgets. While it often lags slightly behind the official wxWidgets
38releases, it also offers a number of features via pure Python
39extensions that are not available in other language bindings. There
40is an active wxPython user and developer community.
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000041
Andrew M. Kuchling2eaa5422010-05-27 13:22:53 +000042Both wxWidgets and wxPython are free, open source, software with
43permissive licences that allow their use in commercial products as
44well as in freeware or shareware.
45
Georg Brandl6728c5a2009-10-11 18:31:23 +000046
47Qt
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000048---
Georg Brandl6728c5a2009-10-11 18:31:23 +000049
Ezio Melotti8f899282013-10-06 01:02:09 +030050There are bindings available for the Qt toolkit (using either `PyQt
Georg Brandl6e0b44e2016-02-26 19:37:12 +010051<https://riverbankcomputing.com/software/pyqt/intro>`_ or `PySide
Serhiy Storchakafdcb5352016-04-11 12:18:56 +030052<https://wiki.qt.io/PySide>`_) and for KDE (`PyKDE4 <https://techbase.kde.org/Languages/Python/Using_PyKDE_4>`__).
Ezio Melotti8f899282013-10-06 01:02:09 +030053PyQt is currently more mature than PySide, but you must buy a PyQt license from
Georg Brandl6e0b44e2016-02-26 19:37:12 +010054`Riverbank Computing <https://www.riverbankcomputing.com/commercial/license-faq>`_
Ezio Melotti8f899282013-10-06 01:02:09 +030055if you want to write proprietary applications. PySide is free for all applications.
56
57Qt 4.5 upwards is licensed under the LGPL license; also, commercial licenses
Georg Brandl6e0b44e2016-02-26 19:37:12 +010058are available from `The Qt Company <https://www.qt.io/licensing/>`_.
Georg Brandl6728c5a2009-10-11 18:31:23 +000059
60Gtk+
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000061----
Georg Brandl6728c5a2009-10-11 18:31:23 +000062
63PyGtk bindings for the `Gtk+ toolkit <http://www.gtk.org>`_ have been
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000064implemented by James Henstridge; see <http://www.pygtk.org>.
Georg Brandl6728c5a2009-10-11 18:31:23 +000065
66FLTK
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000067----
Georg Brandl6728c5a2009-10-11 18:31:23 +000068
69Python bindings for `the FLTK toolkit <http://www.fltk.org>`_, a simple yet
70powerful and mature cross-platform windowing system, are available from `the
71PyFLTK project <http://pyfltk.sourceforge.net>`_.
72
Georg Brandl6728c5a2009-10-11 18:31:23 +000073OpenGL
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000074------
Georg Brandl6728c5a2009-10-11 18:31:23 +000075
76For OpenGL bindings, see `PyOpenGL <http://pyopengl.sourceforge.net>`_.
77
78
79What platform-specific GUI toolkits exist for Python?
Andrew M. Kuchling3bfd0312010-05-27 13:30:09 +000080========================================================
Georg Brandl6728c5a2009-10-11 18:31:23 +000081
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000082By installing the `PyObjc Objective-C bridge
Georg Brandlfa55a312014-10-29 09:24:54 +010083<https://pythonhosted.org/pyobjc/>`_, Python programs can use Mac OS X's
Ned Deily4624b002014-04-12 09:31:28 -070084Cocoa libraries.
Georg Brandl6728c5a2009-10-11 18:31:23 +000085
86:ref:`Pythonwin <windows-faq>` by Mark Hammond includes an interface to the
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +000087Microsoft Foundation Classes and a Python programming environment
88that's written mostly in Python using the MFC classes.
Georg Brandl6728c5a2009-10-11 18:31:23 +000089
90
91Tkinter questions
92=================
93
94How do I freeze Tkinter applications?
95-------------------------------------
96
97Freeze is a tool to create stand-alone applications. When freezing Tkinter
98applications, the applications will not be truly stand-alone, as the application
99will still need the Tcl and Tk libraries.
100
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +0000101One solution is to ship the application with the Tcl and Tk libraries, and point
Georg Brandl6728c5a2009-10-11 18:31:23 +0000102to them at run-time using the :envvar:`TCL_LIBRARY` and :envvar:`TK_LIBRARY`
103environment variables.
104
105To get truly stand-alone applications, the Tcl scripts that form the library
106have to be integrated into the application as well. One tool supporting that is
107SAM (stand-alone modules), which is part of the Tix distribution
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +0000108(http://tix.sourceforge.net/).
109
110Build Tix with SAM enabled, perform the appropriate call to
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100111:c:func:`Tclsam_init`, etc. inside Python's
Andrew M. Kuchling5c4e6eb2009-10-13 16:11:49 +0000112:file:`Modules/tkappinit.c`, and link with libtclsam and libtksam (you
113might include the Tix libraries as well).
Georg Brandl6728c5a2009-10-11 18:31:23 +0000114
115
116Can I have Tk events handled while waiting for I/O?
117---------------------------------------------------
118
Terry Jan Reedy84924e62015-05-17 14:49:20 -0400119On platforms other than Windows, yes, and you don't even
120need threads! But you'll have to restructure your I/O
Sandro Tosi98ed08f2012-01-14 16:42:02 +0100121code a bit. Tk has the equivalent of Xt's :c:func:`XtAddInput()` call, which allows you
Georg Brandl6728c5a2009-10-11 18:31:23 +0000122to register a callback function which will be called from the Tk mainloop when
Terry Jan Reedy84924e62015-05-17 14:49:20 -0400123I/O is possible on a file descriptor. See :ref:`tkinter-file-handlers`.
Georg Brandl6728c5a2009-10-11 18:31:23 +0000124
125
126I can't get key bindings to work in Tkinter: why?
127-------------------------------------------------
128
129An often-heard complaint is that event handlers bound to events with the
130:meth:`bind` method don't get handled even when the appropriate key is pressed.
131
132The most common cause is that the widget to which the binding applies doesn't
133have "keyboard focus". Check out the Tk documentation for the focus command.
134Usually a widget is given the keyboard focus by clicking in it (but not for
135labels; see the takefocus option).
136
137
138