Previous: Procedures
Up: Widgets
Next: Using radio buttons
Previous Page: Procedures
Next Page: Using radio buttons
With the widgets so far described, it is possible to make a small data entry example. It is not very sophisticated, and carries out no data validation, but illustrates the ease of creating a data entry screen.
The following personal data is to be collected:
Surname, First names, Address, Telephone number, Sex, Age
For the Surname, First names, Telephone number and Age, entry widgets will be used. For the Address, a text widget will be used, and for the Sex input, a radiobutton will be used.
The layout of the display will have a column of labels to the left, with a column of entry and other widgets to the right.
This can be implemented using two frames, one for each column.
Additionally, when all the data is entered, a Save button will store the data in a file, for future use, either by a TCL program, or by an external program, and a Quit button wll cause the program to finish.
#Define 2 frames - 1 to contain the columns, and 1 to contain the
#Save and Quit button.
frame .fr1
frame .fr2
#Define two frames for the columns
frame .fr1.c1
frame .fr1.c2
#define labels
label .fr1.c1.lab1 -text "Surname"
label .fr1.c1.lab2 -text "First Names"
label .fr1.c1.lab3 -text "Address"
label .fr1.c1.lab4 -text "Telephone Number"
label .fr1.c1.lab5 -text "Sex"
label .fr1.c1.lab6 -text "Age"
#Now define the other widgets for the data
entry .fr1.c2.surname1 -width 30 -relief sunken -textvariable surname
entry .fr1.c2.firstname1 -width 30 -relief sunken
-textvariable firstnames
entry .fr1.c2.address1 -width 30 -relief sunken
-textvariable address
entry .fr1.c2.telephone -width 20 -relief sunken -textvariable telno
entry .fr1.c2.age1 -width 20 -relief sunken -textvariable age
entry .fr1.c2.sex1 -width 20 -relief sunken -textvariable sex
#We shall defer consideration of the use of radio buttons for
#the next section, so we will simply allocate an entry widget for
#Sex for the moment.
#Now define the buttons for saving and quitting
button .fr2.button1 -text "Save" -command {savecommand}
button .fr2.button2 -text "Quit" -command {exit}
#Note: the command for save could be done by system
#dependent commands, or by TCL commands.
#For portability it is best to package them into a TCL procedure
#Now pack all the widgets together - first within each frame
#Column 1 widgets
pack .fr1.c1.lab1 .fr1.c1.lab2 .fr1.c1.lab3 .fr1.c1.lab4
.fr1.c1.lab5 .fr1.c1.lab6 -pady 1m
#Column 2 widgets
pack .fr1.c2.surname1 .fr1.c2.firstname1 .fr1.c2.address1
.fr1.c2.telephone .fr1.c2.sex1 .fr1.c2.age1
-padx 3m -pady 1m -fill x
#Now pack frames side by side
pack .fr1.c1 .fr1.c2 -side left
#now develop the procedure for saving the data file
proc savecommand {} {
#external variables have to be declared as "global"
global surname firstnames address telno sex age
set file [open /tmp/outfile w]
puts $file $surname
puts $file $firstnames
puts $file $address
puts $file $telno
puts $file $sex
puts $file $age
close $file
}
#Note: In Unix this could have been written as
# proc savecommand {} {
# global surname firstnames address telno sex age
# exec echo $surname > /tmp/outfile
# exec echo $firstnames >> /tmp/outfile
# exec echo $address >> /tmp/outfile
# exec echo $telno >> /tmp/outfile
# exec echo $sex >> /tmp/outfile
# exec echo $age >> /tmp/outfile
# }
# And finally pack the Save and Quit buttons at the bottom
pack .fr2.button1 .fr2.button2 -padx 2m -pady 1m -fill x
pack .fr1
pack .fr2 -padx 2m -pady 1m -fill x