The tkinter GUI Module and Tools

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.

tkinter Example

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()

tkinter Core Widgets

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 ...

Get Python Pocket Reference, 4th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.