O'Reilly logo

Python Pocket Reference, Second Edition by Mark Lutz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Tkinter GUI Module and Tools

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

Tkinter Example

In Tkinter scripts, widgets are customizable classes (e.g., Button, Frame), options are keyword arguments (e.g., text="press"), and composition is 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-20 lists the primary widget classes in the Tkinter module. These are true Python classes that may 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( )).

Table 1-20. Tkinter core widget classes

Widget class

Description

Label

Simple message area

Button

Simple labeled pushbutton widget

Frame

Container for attaching and arranging other widget objects

Toplevel, Tk

Top-level windows managed ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required