Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1
FIGURE 18.3 Demonstrating the Button event method.

A typical GUI program contains lots of different event handlers, one for each widget that can trigger
an event. Sometimes trying to keep track of all the event handles can be challenging. This is where
using the docstring feature can come in handy. It’s always a good idea to place a one-line
docstring value in each event handler method to help describe what it does, as well as what
widget triggers it, as shown here:


Click here to view code image


def display(self):
"""Event handler for the button to display text in the command line"""
print('The button was clicked!')

You don’t have to get too fancy with the docstring. Just place enough information in it to help link
up the event handler to the appropriate widget.


Exploring the tkinter Widgets


Now that you’ve seen the basics of how widgets interact in a GUI program, you’re ready to see the
different types of widgets available for you to use. Each widget contains attributes and methods that
you can use to customize the widget in your window. The following sections show some of the most
popular widgets used in Python GUI programs and how to use them.


Using the Label Widget


The Label widget allows you to place text inside the window. This widget is often used to identify
other widgets, such as Entry or Textbox areas, to help your program users know how to interact
with the widgets.


To add a Label widget to your window, you define the text to display with the text parameter, as
shown here:

Free download pdf