Chapter 5  Widgets & Some Options

Do I need a special widget to fix my dishwasher?

Widgets are often components of a GUI (Graphical User Interface).  Widgets can be used for almost every task.  I like to think of widgets as objects that have properties that you can tweak.  Up to this point, we have only discussed the message widget in detail.  I think the time is right to list some other widgets.  You may choose to make use of these additional widgets to tackle different tasks.  The first thing that needs to be addressed is to use special widgets that will allow us to group other widgets.  A very common widget for grouping is the toplevel widget.  Some widgets that might be used to display information are label, message and text widgets.  Widgets used to collect information and offer choices are button, checkbutton, entry, listbox, radiobutton and scale widgets.  For more specifics about these widgets and general manual information examine: http://www.scriptics.com/man/ .  Widgets differ in their purpose, however, they also have some common ground.  Let's take a look at some of these widgets in the following example:

######################################
# Written by Nelson
# Example ch5p11.tcl
# This program offers an example of
# various widgets:
# toplevel
# label
# message
# text
# button
# checkbutton
# entry
# listbox
# radiobutton
# scale
######################################
####################
# Procedure section
####################
######################################################
# Procedure clean will remove old widgets
######################################################
proc clean {} {
 # if [winfo exists ###] {destroy ###}
 # Clean these up if you want.
 # toplevel
 # label
 # message
 # text
 # button
 # checkbutton
 # entry
 # listbox
 # radiobutton
 # scale
}

##################################
# The main code section.
##################################
#clean
toplevel .top1
label  .label1
message .msg1
text .text1
button .button1
checkbutton .check1
entry .top1.entry1
listbox .top1.list1
radiobutton  .top1.rad1
scale  .top1.scale1

pack  .msg1 .text1 .button1 .check1 .top1.entry1 .top1.list1 .top1.rad1 .top1.scale1
 

How do I tweak a widget already created?

The way you can change options on widgets is by using the configure option.  Examine the example:

######################################
# Written by Nelson
# Example ch5p12.tcl
# This program offers an example of
# the use of -configure option.
# I managed to sneak a couple of buttons
# in this example.;)
######################################
if [winfo exists .msg] {destroy .msg}
if [winfo exists .button1] {destroy .button1}
if [winfo exists .button2] {destroy .button2}

message .msg -width 15c -text "Which Dog Should Run?"
# Notice the "\"  It allows a command to span multiple lines.
# I am using it here to format the text the way I want it.
button .button1 -command {.msg configure -text "Run Lassie! Run!"} \
 -text "Lassie"
button .button2 -command {.msg configure -text "Run Bingo! Run!"} \
 -text "Bingo"
pack .msg .button1 .button2

What should I get from this code?

The example ch5p12.tcl code has some new options.  The command option was introduced.  Notice how we spanned multiple lines with the \ symbol.  The command option executes a command from a widget.

Assignment 5.12
Modify the example ch5p12.tcl code to include the names Rover, Sissy, and Old Yellow.  Also include a reset button to restore the default text in the .msg widget.
 

How can I use this command option to my advantage?

Since you can now execute commands with widgets, the possibilities are endless.  For example, rather than hard coding the names of our favorite dogs in the program we could configure an entry widget.  This widget can collect the names and then update the message widget.  Take a look at this example:

######################################
# Written by Nelson
# Example ch5p13.tcl
# This program offers an example of
# the use of -configure option and entry widget.
######################################
if [winfo exists .msg] {destroy .msg}
if [winfo exists .button1] {destroy .button1}
if [winfo exists .entry1] {destroy .entry1}

message .msg -width 15c -text "Which Dog Should Run?"

# This is how we will enter our input
entry .entry1 -textvar entry1Var

# Notice the "\"  It allows a command to span multiple lines.
# I am using it here to format the text the way I want it.
button .button1 -command {.msg configure -text "Run $entry1Var! Run Fast!"} \
 -text "Update The Dog!"

pack .msg .entry1 .button1

Again, what should I get from this example?

You should notice the textvar option used with the .msg message widget.  Also notice the power of substitution.

Assignment 5.13
Create a simple calculator that will collect 2 numbers from entry widgets and then perform the operations of addition, subtraction, multiplication and division.
(Hint:  Maybe a button for each operation.)

Does the mod operation work in your program?

Try looking at precision again.  See if you get the same conclusions as we did previously.

Assignment 5.14
Write a program that will collect a number to represent a letter grade following the format below:
A value >= 90
B 80 <= value < 90
C 70 <= value < 80
D 60 <= value < 70
F       value < 60
Display your conversion in a message widget in your program.

How do I reduce the widget clutter?

To allow us to keep our user interfaces organized we need to be able to tweak the location of our widgets.  To accomplish this we will use the place command.  You can control where widgets are on the screen with the place command.  Examine the format in the example below:

######################################
# Written by Nelson
# Example ch5p15.tcl
# This program offers an example of
# the use of -configure option and entry widget.
######################################
if [winfo exists .msg] {destroy .msg}
if [winfo exists .button1] {destroy .button1}
if [winfo exists .entry1] {destroy .entry1}

message .msg -width 15c -text "Which Dog Should Run?"

# This is how we will enter our input
entry .entry1 -textvar entry1Var

# Notice the "\"  It allows a command to span multiple lines.
# I am using it here to format the text the way I want it.
button .button1 -command {.msg configure -text "Run $entry1Var! Run Fast!"} \
 -text "Update The Dog!"
pack .msg .entry1 .button1
# Change these numbers a bit and see what happens;)
place .button1 -x 5 -y 20
place .entry1 -x 140 -y 23

Since the place command will accomplish what we want to do in this book, you might want to read more about placement topics at http://www.scriptics.com/

Assignment 5.15
Write a program that contains a message widget and 2 buttons.  The actions for each button should be as follows:
 button1 = move the message widget to the right by 10
 button2 = move the message widget to the left by 10
 

Can I tune in my favorite station with the radiobutton?

The radiobutton is used to offer choices, including those choices that are mutually exclusive.  For example, let's take a look at how we could use the radiobutton to select the dog we wanted to choose:

######################################
# Written by Nelson
# Example ch5p16.tcl
# This program offers an example of
# the use of radiobutton widget.
######################################
if [winfo exists .msg] {destroy .msg}
if [winfo exists .button1] {destroy .button1}
if [winfo exists .rad1] {destroy .rad1}
if [winfo exists .rad2] {destroy .rad2}
 

message .msg -width 15c -text "Which Dog Should Run?"

# Notice the "\"  It allows a command to span multiple lines.
# I am using it here to format the text the way I want it.
button .button1 -command {.msg configure -text "Run $radVar! Run Fast!"} \ -text "Update The Dog!"
radiobutton .rad1 -text "Rover" -value "Big Rover" -variable radVar
radiobutton .rad2 -text "Sissy" -value "Ye Ole Sissy" -variable radVar

pack .msg .button1 .rad1 .rad2
# Change these numbers a bit and see what happens;)
place .button1 -x 5 -y 20
place .rad1 -x 5 -y 60
place .rad2 -x 5 -y 80
 

You might also want to include commands with your radiobuttons.  Take a look at some modifications to the example chp5p16.tcl:

######################################
# Written by Nelson
# Example ch5p16b.tcl
# This program offers an example of
# the use of radiobutton widget.
######################################
if [winfo exists .msg] {destroy .msg}
if [winfo exists .rad3] {destroy .rad3}
if [winfo exists .rad1] {destroy .rad1}
if [winfo exists .rad2] {destroy .rad2}
 

message .msg -width 15c -text "Which Dog Should Run?"

# Notice the "\"  It allows a command to span multiple lines.
# I am using it here to format the text the way I want it.
radiobutton .rad1 -text "Rover" -value "Big Rover" -variable radVar \
  -command {.msg configure -text "Run $radVar! Run Fast!"}
radiobutton .rad2 -text "Sissy" -value "Ye Ole Sissy" -variable radVar \
  -command {.msg configure -text "Run $radVar! Run Fast!"}
radiobutton .rad3 -text "Clear" -variable radVar \
  -command {.msg configure -text "Which Dog Should Run?"}

pack .msg .rad1 .rad2 .rad3
# Change these numbers a bit and see what happens;)
place .rad3 -x 5 -y 20
place .rad1 -x 5 -y 40
place .rad2 -x 5 -y 60
 
 

Can I cash a check with a checkbutton?

Cashing checks is not the purpose, but allowing the user to control options is ideal for the checkbutton.  Let's take a look at example chp516.tcl modified with checkbuttons to enhance the code a bit:

######################################
# Written by Nelson
# Example ch5p16c.tcl
# This program offers an example of
# the use of checkbutton widget.
######################################
if [winfo exists .msg] {destroy .msg}
if [winfo exists .chk3] {destroy .chk3}
if [winfo exists .chk1] {destroy .chk1}
if [winfo exists .chk2] {destroy .chk2}
if [winfo exists .button1] {destroy .button1}

set defaultMessage "Which Dog Should Run?"
message .msg -width 15c -text $defaultMessage

# Notice the "\"  It allows a command to span multiple lines.
# I am using it here to format the text the way I want it.
button .button1 -command {
 set theMessage ""
 if {$dog1} {set theMessage "$theMessage Run Rover "}
 if {$dog2} {set theMessage "$theMessage Run Ye Ole Sissy "}
 if {$dog3} {set theMessage "$theMessage Run Fast! "}
 if {[string length $theMessage]==0} {
  .msg configure -text $defaultMessage
  } else {
  .msg configure -text $theMessage
  }
 } -text "Update The Dog!"
# end of the button action
checkbutton .chk1 -text "Rover" -variable dog1
checkbutton .chk2 -text "Sissy"  -variable dog2
checkbutton .chk3 -text "Run Fast" -variable dog3

pack .msg .button1 .chk1 .chk2 .chk3
# Change these numbers a bit and see what happens;)
place .button1 -x 5 -y 80
place .chk3 -x 5 -y 20
place .chk1 -x 5 -y 40
place .chk2 -x 5 -y 60
 

Assignment 5.16
Write a program that collects a value and offers a choice of performing 3 different functions on that value (only 1 operation at a time):
1.)Floor function
2.)Absolute value function
3.)The function f(x)=x^2

Assignment 5.17
Write a program that collects a value and offers a choice of performing 3 different functions on that value in the following order.  If more than 1 operation is selected follow the below order:
1.)Floor function
2.)Absolute value function
3.)The function f(x)=x^2

Assignment 5.18
If a number goes through all 3 functions from Assignment 5.17, what type of number is the end result?


Next Section

Table of Contents