Section 15.3 GUI Widgets
As we discussed in the introduction, a GUI program allows a user to interact with a computer program using a pointing device that manipulates small pictures called
widgets. The first task of a GUI program is to create the widgets needed for a program’s interface. Each widget is designed for specific purposes and your program will be more user friendly if you use each widget according to its intended purpose.
Widgets are basically images on a computer screen and they have a “look-and-feel” depending on the details of how the image is drawn. The “look-and-feel” of a widget is typically controlled by the operating system. For example, GUI programs on a Macintosh computer typically look different from programs on a Microsoft Windows computer. The
tkintermodule implements two versions of widgets: one is “generic,” which makes widgets look the same regardless of what computer your program is running on, and the other implements widgets that emulate a computer’s “look-and-feel”. How you import the
tkintermodule determines which widgets are defined. Using the import statements shown below, the standard convention uses the name
tkto access the “generic” widgets and the name
ttkto access the stylized, “look-and-feel” widgets. You always need to import the
tkfunctionality because that allows you to create an application window. You can import the
ttkfunctionality if you want “look-and-feel” widgets. You can inter-mix the
ttkwidgets in an interface if you so choose.
# To use the "generic" widgets import tkinter as tk # To use the stylized, "look-and-feel" widgets from tkinter import ttk
The following two charts list the standard, pre-defined widgets in the
The following widgets are used for user input. In some cases you have a choice between the
ttkversions. In other cases you must use the
tkversion because the equivalent
ttkversions don’t exist.
||Execute a specific task; a “do this now” command.|
||Implements toplevel, pulldown, and popup menus.|
||Displays popup or pulldown menu items when activated.|
||Creates a popup menu, and a button to display it.|
||Enter one line of text.|
||Display and edit formatted text, possibly with multiple lines.|
||Set on-off, True-False selections.|
||Allow one-of-many selections.|
||Choose one or more alternatives from a list.|
||Combines a text field with a pop-down list of values.|
||Select a numerical value by moving a “slider” along a scale.|
The following figure shows examples of these widgets. You can download and run this python program, all_user_input_widgets.py 1 , to interact with the widgets.
widgetsdisplay information to a user, but have no user interaction:
||Display static text or an image.|
||Display static multi-line text.|
||Displays a horizontal or vertical separator bar.|
||Shows the status of a long-running operation.|
||Displays a hierarchical collection of items.|
You do not need to memorize the above lists, but you should probably re-read the lists again so that you are familiar with what is possible in a TKinter GUI interface. (Note that the TKinter module is customizable, which means that you can create your own widgets, but that is beyond what we will study in these lessons.)
You have attempted of activities on this page.