Tkinter makes it easy to build simple GUI applications. You import
Tkinter, create, configure, and position the widgets you want, then enter the
Tkinter main loop. Your application becomes event-driven: the user interacts with the widgets, causing events, and your application responds via the functions you installed as handlers for these events.
The following example shows a simple application that exhibits this general structure:
import sys, Tkinter Tkinter.Label(text="Welcome!").pack( ) Tkinter.Button(text="Exit", command=sys.exit).pack( ) Tkinter.mainloop( )
The calls to
Button create the respective widgets and return them as results. Since we specify no parent windows,
Tkinter puts the widgets directly in the application’s main window. The named arguments specify each widget’s configuration. In this simple case, we don’t need to bind variables to the widgets. We just call the
pack method on each widget, handing control of the widget’s layout to a layout manager object known as the packer. A layout manager is an invisible component whose job is to position widgets within other widgets (known as container or parent widgets), handling geometrical layout issues. The previous example passes no arguments to control the packer’s operation, which lets the packer operate in a default way.
When the user clicks on the button, the
command callable of the
Button widget executes without arguments. The example passes function
sys.exit as the argument named