94 CHAPTER 7. GRAPHICAL USER INTERFACE DESIGN IN SCHEME
make a window disappear, or
draw some shapes on a canvas in a window, or
read a textfield, check to see if the textfield contains the correct answer
and write an approprite response in another textfield.
These examples all have the property that an action consists of some small
number of basic steps which operate on the components of the GUI. In the next
few subsections, we will give examples of each of these types of programs and
in the process introduce you to more Scheme.
7.18
Calculator Programs
Here we give some examples of programs that get numeric input from com
ponents and use that to compute some numerical result. Our example is the
Fahrenheit to Celsius converter in Figure 7.3.
Lets look over this application. First, the GUI itself uses a border layout
with a menubar. The menubar has a File menu with a quit menuitem and when
the user clicks the quit item, the window is hidden.
(menubar
(menu "File"
(menuitem "quit"
(action (lambda(e) (.hide win))))))
The main part of the window has a label in the north, a labelled input textfield
in the center, and two buttons with actions in the south. The actions are the
trickiest part of this program. Lets look at them in detail.
(let* (
(f (readexpr (t "x")))
(c (* (/ 5.0 9.0) ( f 32.0)))
)
(writeexpr (t "x") c))
This action starts by reading the expression in the textfield (t x ). It then
uses that number, f, to compute the celsius equivalent. This is done by first
subtracting 32 and then multiplying the result by 5/9. Once f and c have been
computed, the value of c is written back onto the textfield with the x tag.
Rather than using two buttons, we could have used a choice component
as follows:
(t "operation" (choice "convert to F" "convert to C"
(action (lambda(e)
(case (readstring (t "operation"))
(("convert to F")
(let* ( (f (readexpr (t "x")))