Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous contents index
Next: Listbox and Scrollbar Up: Glish/Tk Previous: Frame

Subsections



Button

Buttons are the simplest widgets which actually do something, i.e. generate events. You create buttons to allow the user to click on an area of the screen to cause some action. In Glish/Tk, the action which the button itself causes is the generation of a press event. The Glish script handles this event as it does any other event.

Plain Buttons

Plain buttons are the simplest kind of buttons. They have no state, and the only thing that happens when the user presses them is that a press event is generated. Here is an example of how plain buttons are created:
    fme := frame( )
    b1 := button(fme,'Button One')
    b2 := button(fme,'Button Two')
    whenever b1->press do
        print 'Button One Pressed'
    whenever b2->press do
        print 'Button Two Pressed'
This example generates a simple dialog containing two buttons, and when the user presses either of the buttons, a message is printed by the whenever statements. The dialog box which this creates looks like the one in Figure 11.2.

Figure 11.2: Button
\begin{figure}
\centerline{\psfig{figure=tkbutton.eps,scale={0.7}}}\end{figure}

It is simply a frame with two buttons. In addition to the parameters shown in this example, buttons accept many other construction parameters. These are summarized in Table 11.4.


Table 11.4: Button Construction Parameters
Parameter Default Values Description
parent widget parent of the button
text 'button' string text label for button
type 'plain' 'plain' 'radio' 'check' 'menu' button type
padx 7 dimension horizontal padding around text
pady 3 dimension vertical padding around text
width 0 integer width in character units
height 1 integer height in character units
justify 'center' 'center' 'left' 'right' justification of text
font '' X font font of text
relief 'raised' 'flat' 'ridge' 'raised' 'sunken' 'groove' edge relief
borderwidth 2 dimension border width
foreground 'black' X color color of text
background 'lightgrey' X color background color
disabled F boolean is inactivated?
value T Glish value value returned with press event
anchor 'c' 'c' 'n' 's' 'e' 'w' 'ne' 'nw' 'se' 'sw' location of text
fill 'none' 'x' 'y' 'both' 'none' how to expand when resized
bitmap '' path use specified bitmap file instead of text
group parent widget only for radio; indicates how radio functionality is grouped
hlcolor '' X color highlight color with focus
hlbackground '' X color highlight color without focus
hlthickness [] dimension hightlight border thickness

In this example, if fme is constructed with side set to left, then the buttons are arranged horizontally instead of vertically. In fact, if the frame is sent side events:

    fme->side('left')         # arrange left to right
    fme->side('right')        # arrange right to left
    fme->side('bottom')       # arrange bottom to top
the buttons can be dynamically rearranged. In an analogous way, the text of the buttons can also be changed:
    b1->text('First Button')
    b2->text('Second Button')
The other events which buttons accept are similar to the construction parameters; they are outlined in Table 11.5. The only event which buttons generate is the press event, and this is the hook through which buttons cause actions in a script.


Table 11.5: Button Events
Event I/O Values Description
anchor $ \rightarrow$ 'c' 'n' 's' 'e' 'w' 'ne' 'nw' 'se' 'sw' set location of text
background $ \rightarrow$ X color change background color
bind $ \rightarrow$ <X> <G> associate Xevent <X> with Glish event <G>
bitmap $ \rightarrow$ string set path to bitmap file
borderwidth $ \rightarrow$ dimension change border width
disable $ \rightarrow$ disable widget, must be balanced by enable
disabled $ \rightarrow$ boolean set state, disable ( T) or enable ( F)
enable $ \rightarrow$ enable widget, must be balanced by disable
font $ \rightarrow$ X font set font for text
foreground $ \rightarrow$ X color change foreground color
height $ \rightarrow$ integer set height (in character units)
hlbackground $ \rightarrow$ X color highlight color without focus
hlcolor $ \rightarrow$ X color highlight color with focus
hlthickness $ \rightarrow$ dimension hightlight border thickness
justify $ \rightarrow$ 'center' 'left' 'right' set justification for text
padx $ \rightarrow$ dimension set horizontal padding around text
pady $ \rightarrow$ dimension set vertical padding around text
press $ \leftarrow$ Glish value indicates that button was pressed
relief $ \rightarrow$ 'flat' 'ridge' 'raised' 'sunken' 'groove' change border relief
state $ \Leftrightarrow$ boolean set state for button ( check and radio only)
text $ \rightarrow$ string set text
width $ \rightarrow$ integer set width (in character units)

As shown in Table 11.4, there are several different types of buttons - plain, radio, check, and menu. These are set with the type parameter. The example above shows how plain buttons work. Below you will see how other buttons work.


Check Buttons

Check buttons are identical to plain buttons, but check buttons have state. Their state is indicated by their appearance. Check buttons allow the user to select multiple items from a list. Here is an example of a check box:
    root := frame( side='left' )
    fbox := frame( root, borderwidth=0 )
    box := [=]
    for (i in "one two three")
        {
        box[i] := button(fbox,paste('button',i),type='check',value=i)
        whenever box[i]->press do
            print "check button", $value, "pressed"
        }
When the user clicks on check buttons, the appearance of the button changes to indicate the state of the button. Note that the the value parameter to button is used to permit later differientation of the buttons. The graphic that this script creates is shown in Figure 11.3. Note, however, that the second button has been pressed by the user.

Figure 11.3: Check Button
\begin{figure}
\centerline{\psfig{figure=tkcheckb.eps,scale={0.7}}}\end{figure}

The script prints ``check button two pressed'' in response to the selection of the second check button. This action is again accomplished with a whenever statement. Each time one of the check buttons is pressed a press event is generated.

You can query the state of a check button with the state event:

    print box[2]->state()
Since the second button is selected once, the output of this statement is T. The state event is also used to set the state. So if you want to unselect the second check button, you do something like:
    box[2]->state(F)
and the the appearance of the second button's check box changes to look like the checkboxes of the other two check buttons.


Radio Buttons

Radio buttons are very similar to check buttons with the exception that radio buttons are mutually exclusive. This means that the state of only one radio button in a group can have its state set to T. By default, a group is defined to be all radio buttons within a given frame, but the group parameter can be used to change this grouping.

To change the dialog in Figure 11.3 to use radio buttons instead of check buttons, you can do the following:

    for (i in "one two three")
        {
        box[i] := button(fbox,paste('button',i),type='radio',value=i)
        whenever box[i]->press do
            print "check button", $value, "pressed"
        }
    box[i]->state(T)
Notice that you use the same toplevel frame from the example above, but when each box[i] is reassigned, the previous widget is deleted. Assume you wanted to have an extra column of radio buttons, you can do it as follows:
    nf := frame(root, borderwidth=0)
    for (i in "four five six")
        {
        box[i] := button(nf,paste('button',i),type='radio',
                         value=i,group=fbox)
        whenever box[i]->press do
            print "check button", $value, "pressed"
        }
After running these two small scripts, the dialog Figure 11.3 will change dynamically to look like Figure 11.4.

Figure 11.4: Radio Button
\begin{figure}
\centerline{\psfig{figure=tkradiob.eps,scale={0.7}}}\end{figure}

Here again, a press event is generated each time a user selects one of the buttons, and the state of the buttons can be set and queried with the state event. Now, however, selecting button five, for example, causes the previously selected button, here button three, to be unselected. This is the behavior of radio buttons. Specifying the group for the second column of radio buttons make the two columns act as one group.


Menu Buttons

Like radio and check buttons, menu buttons are more complicated than plain buttons. Menu buttons do not have state, but rather their action when pressed is to display more buttons. This allows you to build menus containing all of the buttons discussed above.

As before, a button is indicated to be a menu by setting the type parameter. Here is a simple example:

    top := frame()
    bar := frame(top,side='left',expand='x')
    file := button(bar,'File',type='menu')
    opt := button(bar,'Options',type='menu')
    rec := [=]
    rec['space'] := frame(bar,width=150,height=1)
    help := button(bar,'Help',type='menu')
    for (i in "A B C")
        {
        rec[i] := button(file,paste('File',i))
        rec[len(rec)+1] := button(opt,paste('Opt',i))
        rec[len(rec)+1] := button(help,paste('Help',i))
        }
In this example, there are a couple of things to note. First a frame, bar, is created to contain all of the menus. This is how a menubar is created in Tk. Note that the expand parameter is set to 'x' indicating that bar will only expand horizontally. This is to prevent the frame containing the menu buttons from expanding vertically if the user resizes the window. Next, two menu buttons, file and opt, are placed in bar, and because of the way bar is constructed, these menu buttons are organized left to right. Next a frame is used to space the next menu button out to the end of the dialog, and then another menu, help, is added to the menubar. The loop simply fills each of the menu buttons with entries which are simply more buttons. The key thing to notice is that the parent of these buttons is a menu button.

Figure 11.5: Menu Button
\begin{figure}
\centerline{\psfig{figure=tkmenu.eps,scale={0.7}}}\end{figure}

Entries in a menu button are the only case in Glish/Tk where a widget can have a parent which is not a frame. Figure 11.5 shows what the menubar created above looks like when the user selects, i.e. presses, the ``Options'' menu button and selects the second menu element. The selection by the user of entries in a menu button are caught using whenever statements in the same way as regular buttons. For example:
    whenever rec['A']->press do
        print "Got It!"
causes ``Got It!'' to be printed by the interpreter whenever the first entry in the file menu button is selected.


next up previous contents index
Next: Listbox and Scrollbar Up: Glish/Tk Previous: Frame   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15