15.27. Common Widget Properties¶
Each widget has a set of properties that define its visual appearance on the
computer screen and how it responds to user events. There is a set
of properties that all tk
widgets have in common. Some of these are shown in
the following table.
Common Widget Properties |
Description |
---|---|
|
Background color. |
|
Foreground color. |
|
Width in pixels |
|
Height in pixels |
|
The size of the border in pixels. |
|
Text displayed on the widget. |
|
The font used for text on the widget. |
|
The shape of the cursor when the cursor is over the widget. |
|
The color of the text when the widget is activated. |
|
The color of the background when the widget is activated. |
|
An image to be displayed on the widget. |
You can treat a widget object as a dictionary and use the property names
as keys to access and change the property values. For example, to change the
background color and width of a widget whose object variable is named sam
,
you could do this:
sam = tk.Button(application_window, text="Sam's Button")
sam['bg'] = 'red'
sam['width'] = 60 # pixels
15.28. Specific Widget Properties¶
Each type of widget has properties specific to its intended use. Here are some examples:
Specific Widget Properties |
Applies to |
Description |
---|---|---|
|
Checkbutton Radiobutton Entry Spinbox Text |
An Tk object that will be changed by interaction with the widget. |
|
Scale |
The starting value of a |
|
Scale |
The ending value of a |
|
Scale |
HORIZONTAL or VERTICAL |
|
Scale |
The increment amount along a |
Some widgets have a “variable” that a user manipulates. For such widgets you must create a
special tk object that stores the widget’s value. Basically you are creating
a non-visible widget that can have events associated with it.
You can access the widget’s value through .set(new_value)
and .get()
methods on the variable object
. There are four types of variable objects
:
tk variable object |
Description |
---|---|
|
A tk object that holds a single Boolean value |
|
A tk object that holds a single integer value |
|
A tk object that holds a single double value |
|
A tk object that holds a single string value |
Let’s take a tk.Checkbutton
as an example and walk through the steps needed
to get and set its “value”. You need to do three things:
Create a
tk.BooleanVar
object. (Actually any type ofvariable object
can be used.)Give the
tk.BooleanVar
object the initial value you want for the checkbutton.Assign the
variable
attribute of the checkbutton to thetk.BooleanVar object
.
In code this looks like this:
checkbutton_value = tk.BooleanVar()
checkbutton_value.set(True) # The checkbutton will be "checked"
my_checkbutton = tk.Checkbutton(parent_widget, text="Example check box")
my_checkbutton['variable'] = checkbutton_value
If you want the value of the checkbutton at any time while your program is
running, you get
the value of the variable object
like this:
the_current_value_of_the_checkbutton = checkbutton_value.get()
And you can change the state of the checkbutton at any time using a call to
.set()
like this:
checkbutton_value.set(FALSE) # set the checkbutton to "un-checked"
15.29. Widget Attributes¶
The following web pages provide a good description of the properties that can be modified for individual widgets: