tkinter
(named Tkinter
in
Python 2.X, and a module package in Python 3.0) is a portable graphical
user interface (GUI) construction library shipped with Python as a
standard library module. tkinter
provides an object-based interface to the open source Tk library and
implements native look and feel for Python-coded GUIs on Windows, X-Windows,
and Mac OS. It is portable, simple to use, well documented, widely used,
mature, and well supported. Other portable GUI options for Python such
as wxPython and PyQT are
third-party extensions with richer widget sets but generally more
complex coding requirements.
In tkinter
scripts, widgets are customizable classes (e.g., Button
, Frame
), options are keyword arguments (e.g., text="press"
), and composition refers to object embedding, not
pathnames (e.g., Label(top,...)
):
from tkinter import * # widgets, constants def msg(): # callback handler print('hello stdout...') top = Frame() # make a container top.pack() Label(top, text="Hello world").pack(side=TOP) widget = Button(top, text="press", command=msg) widget.pack(side=BOTTOM) top.mainloop()
Table 1-21 lists the primary widget classes in the tkinter
module. These are true Python
classes that can be subclassed and embedded in other objects. To
create a screen device, make an
instance of the corresponding class, configure it, and
arrange it with one of the geometry manager interface methods (e.g.,
Button(text='hello').pack()
). In
addition to Table 1-21’s
classes, the tkinter
module
provides a large set of predefined names (a.k.a. constants) used to
configure widgets (e.g., RIGHT
,
BOTH
, YES
); these are automatically loaded from
tkinter.constants
(Tkconstants
in Python 2.X).
Table 1-21. Module tkinter core widget classes
Widget class | Description |
---|---|
| Simple message area |
| Simple labeled pushbutton widget |
| Container for attaching and arranging other widget objects |
| Top-level windows managed by the window manager |
| Multiline text-display field (label) |
| Simple single-line text entry field |
| Two-state button widget, used for multiple-choice selections |
| Two-state button widget, used for single-choice selections |
| A slider widget with scalable positions |
| Image object for placing full-color images on other widgets |
| Image object for placing bitmap images on other widgets |
| Options associated with
a |
| Button that opens a
|
| Bar for scrolling other
widgets (e.g., |
| List of selection names |
| Multiline text browse/edit widget, support for fonts, etc. |
| Graphics drawing area: lines, circles, photos, text, etc. |
| Composite: pull-down selection list |
| A multipane window interface |
| A labeled frame widget |
| A multiple selection widget |
| Python 2.X name
(available in module |
| Python 2.X name
(available in module |
showinfo(title=None, message=None, **options) showwarning(title=None, message=None, **options) showerror(title=None, message=None, **options) askquestion(title=None, message=None, **options) askokcancel(title=None, message=None, **options) askyesno(title=None, message=None, **options) askretrycancel(title=None, message=None, **options)
class Open class SaveAs class Directory askopenfilename(**options) asksaveasfilename(**options) askopenfile(mode="r", **options) asksaveasfile(mode="w", **options) askdirectory(**options)
The common dialog call options are defaultextension
(added to filename if not
explicitly given), filetypes
(sequence of (label, pattern)
tuples), initialdir
(initial
directory, remembered by classes), initialfile
(initial file), parent
(window in which to place the
dialog box), and title
(dialog
box title).
Table 1-22 lists some commonly used tkinter interfaces and tools beyond the core widget class and standard dialog set.
Table 1-22. Additional tkinter tools
Tool category | Available tools |
---|---|
tkinter linked-variable classes |
|
Geometry management methods |
|
Scheduled callbacks | Widget |
Other tkinter tools | Clipboard access;
|
tkinter extensions (search the Web) | PMW: more widgets; PIL: images; tree widgets, font support, drag-and-drop, tix widgets, ttk themed widgets, etc. |
Table 1-23 compares Python’s tkinter
API
to the base Tk library as exposed by the Tcl language. In general,
Tcl’s command strings map to objects in the Python language.
Specifically, in Python’s tkinter
,
the Tk GUI interface differs from Tcl in the following ways:
Widgets are created as class instance objects by calling a widget class.
Parents are previously created objects, passed to widget class constructors.
Options are constructor or config
keyword arguments, or indexed
keys.
Widget operations (actions) become tkinter
widget class object
methods.
Callback handlers are any callable object: function,
method, lambda
, class with
__call__
method, etc.
Widgets are extended using Python class inheritance mechanisms.
Interfaces are constructed by attaching objects, not by concatenating names.
Variables associated with widgets are tkinter
class objects with methods.
Table 1-23. Tk-to-tkinter mappings
Operation | Tcl/Tk | Python/tkinter |
---|---|---|
Creation |
|
|
Masters |
|
|
Options |
|
|
Configure |
|
|
Actions |
|
|
Packing |
|
|