User manual
80
root.title("LED") Objects in Tkinter make different methods for different purposes available. The method
title() in a widget sets the window caption, and in our case, it writes the word LED into the title bar of the
new window.
Each widget can contain multiple objects that are defined separately. Tkinter knows different types of
objects, each of them features different parameters to describe the object's properties. The parameters
separated by commas, are embedded in a bracket after the object type. Since this list may become very long,
each parameter is usually written on a separate command line, so that all parameters are aligned below
each other. Contrary to the indents of loops and queries in Python, these indents of the Tkinter objects are
not mandatory.
Label (root, text = "Please click a button to turn the LED on
or off").pack()
Objects of the type Label are plain text in a widget. These can be modified by the program, but do not offer
any interaction with the user. The first parameter in any Tkinter object is the name of the parent widget,
often of the object’s window. In our case, the only window in the program, the
root widget.
The parameter
text contains the text that should be shown on the label. At the end of the so-called object
definition the extension
.pack() as the method is added to the so-called packer. This packer structures the
object into the dialog box and generates the geometry of the widget.
Button(root,
text=”On",
command=LedOn).pack(side=LEFT)
Objects of the type Button are buttons that the user clicks to trigger a specific action. Here too, the
parameter
text contains the text that will be displayed on the button.
The parameter
command contains a function that is called by the button when clicked. Here no parameters
can be passed on, and the function name is specified without brackets. This button calls the function
LedOn() which turns on the LED.
The method
.pack() may also contain parameters that determine how an object is arranged within the
dialog box.
side=LEFT means that the button is left-aligned and not centred.
Button(root, text=”Off", command=LedOff).pack(side=LEFT)
Following the same pattern, a second button is still created, which turns off the LED using the function
L
LedOff().
Now that all functions and objects are defined, the actual program can begin.