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

Odds and Ends

This section discusses a few remaining minor widgets. These are widgets which are useful, but have a single function. They are simple widgets much like the ones already discussed. These are grouped together in a single section because they are similar in complexity and functionality to widgets discussed previously.

Label

  The label widget does exactly what you would expect; it is used to insert a short text string into the interface. Here is a simple example:

    f := frame(f,side='left')
    l := label(f,'Options:')
    b1 := button(f,'continue')
    b2 := button(f,'quit')
    whenever b2->press do
        exit
This dialog is shown in Figure 10.7.

  figure6813
Figure 10.7: Label

The label is simply placed in the frame first, and then the buttons which it describes are added to the frame. Table 10.8 show the other parameters which can be specified during the creation of a label.

 

Parameter Default Values Description
parent F widget parent of the label
text 'label' string label text
justify 'left' 'center' 'left' 'right' justification of text
padx 4 dimension horizontal padding
pady 2 dimension vertical padding
font '' X font text font
width 0 integer width in character units
relief 'flat' 'flat' 'ridge' 'raised' 'sunken' 'groove' border relief
borderwidth 2 dimension border width
foreground 'black' X color color of text
background 'lightgrey' X color background color
fill 'none' 'x' 'y' 'both' 'none' how to expand when resized
Table 10.8: Label Construction Parameters

   

Table 10.9 shows the events which labels accept, and as you can

 

Event I/O Values Description
backgroundtex2html_wrap_inline15708 X color change background color
bind tex2html_wrap_inline15708<X> <G> associate Xevent <X> with Glish event <G>
borderwidthtex2html_wrap_inline15708 dimension change border width
font tex2html_wrap_inline15708 X font set text font
foregroundtex2html_wrap_inline15708 X color change foreground color
justify tex2html_wrap_inline15708 'center' 'left' 'right' set text justification
padx tex2html_wrap_inline15708 dimension set horizontal padding
pady tex2html_wrap_inline15708 dimension set vertical padding
relief tex2html_wrap_inline15708 'flat' 'ridge' 'raised' 'sunken' 'groove' change border relief
text tex2html_wrap_inline15708 string set label text
width tex2html_wrap_inline15708 integer set width (in character units)
Table 10.9: Label Events

   

see from the table, labels do not generate any events.  

Scale

  The scale widget allows the user to pick a number out of a range. In other toolkits, this has been called a slider. Here is an example of the creation of a scale:

    f := frame(side='left')
    v := scale(f,0,110,text='Volume')
    t := scale(f,0,110,text='Treble')
    b := scale(f,0,110,text='Bass')
The interface which this example creates is shown in Figure 10.8.

  figure6905
Figure 10.8: Scale

The value of the slider is posted with the value event. Here is an example,

    whenever v->value do
        print "Volume:",$value
This whenever will print out a message each time the volume is changed. Note however if you just want to get the last value of the slider, it is accessible as a member of the agent, in this case v.value. This is because the last value of each event name is retained in the agent. As a result, whenever statements are often not needed with scales. Table 10.10 lists the other construction parameters for scales, and Table 10.11 lists the scale events.

 

Parameter Default Values Description
parent widget parent of the scale
start 0 integer starting integer for scale
end 100 integer ending integer for scale
length 110 integer length of scale
text '' string optional label for scale
resolution 1 float resolution of the scale
orient 'horizontal' 'horizontal' 'vertical orientation of scale
width '15' width/height of the thumb
font '' X font text font
relief 'flat' '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
fill '' 'x' 'y' 'both' 'none' how to expand when resized; default: expand with orient
Table 10.10: Scale Construction Parameters

   

 

Event I/O Values Description
backgroundtex2html_wrap_inline15708 X color change background color
bind tex2html_wrap_inline15708<X> <G> associate Xevent <X> with Glish event <G>
borderwidthtex2html_wrap_inline15708 dimension change border width
end tex2html_wrap_inline15708 integer change ending value
font tex2html_wrap_inline15708 X font set text font
foregroundtex2html_wrap_inline15708 X color change foreground color
length tex2html_wrap_inline15708 integer change length of scale
orient tex2html_wrap_inline15708 'horizontal' 'vertical change orientation
relief tex2html_wrap_inline15708 'flat' 'ridge' 'raised' 'sunken' 'groove' change border relief
resolution tex2html_wrap_inline15708 float change resolution of scale
start tex2html_wrap_inline15708 integer change starting value
text tex2html_wrap_inline15708 string set scale label
value tex2html_wrap_inline15710 integer value changed by user
width tex2html_wrap_inline15708 integer change width/height of scale
Table 10.11: Scale Events

   

 

Message

  The message widget can be viewed as a multi-line label. It is essentially a scratch pad for displaying several word messages to the user. For long messages, the text widget is a better choice.

Here is an example of the message widget in use:

    f := frame(side='left')
    b := button(f,'time')
    m := message(f,shell('date'))
    whenever b->press do
        m->text(shell('date'))
This simple example updates the time display in the message widget whenever the button is pressed. This would look like Figure 10.9.

  figure7007
Figure 10.9: Message

In general, the text event is the primary event for the message widget. The other construction parameters are shown on Table 10.12. The message widget itself does not generate any event, but the events which it accepts are shown in Table 10.13.

 

Parameter Default Values Description
parent widget parent of the message
text 'message' string text for message
width 180 dimension width of message
justify 'center' 'center' 'left' 'right' justification of text
font '' X font font of text
padx 7 dimension horizontal padding
pady 3 dimension vertical padding
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
fill 'none' 'x' 'y' 'both' 'none' how to expand when resized
Table 10.12: Message Construction Parameters

   

 

Event I/O Values Description
backgroundtex2html_wrap_inline15708 X color change background color
bind tex2html_wrap_inline15708<X> <G> associate Xevent <X> with Glish event <G>
borderwidthtex2html_wrap_inline15708 dimension change border width
font tex2html_wrap_inline15708 X font set font for text
foregroundtex2html_wrap_inline15708 X color change foreground color
justify tex2html_wrap_inline15708 'center' 'left' 'right' set justification for text
padx tex2html_wrap_inline15708 dimension set horizontal padding around text
pady tex2html_wrap_inline15708 dimension set vertical padding around text
relief tex2html_wrap_inline15708 'flat' 'ridge' 'raised' 'sunken' 'groove' change border relief
text tex2html_wrap_inline15708 string set text
width tex2html_wrap_inline15708 integer set width (in character units)
Table 10.13: Message Events

   

 

Entry

  The entry provides slots where the user can type in text. The script can then retrieve this text when it is needed. Here is a more complex example:

    f := frame()
    ef := frame(f,side='left')
    b := button(ef,'ls')
    en := entry(ef)
    lbf := frame(f,side='left')             # **
    lb := listbox(lbf,width=32)             # **
    sb := scrollbar(lbf)                    # **
    whenever b->press, en->return do
        {
        lb->delete("start","end")
        dir := en->get()
        files := shell('if [ -r',dir,'];then ls -1F',dir,';else exit 1;fi')
        if ( ! files::status )
            lb->insert(files)
        else
            en->insert('*BAD DIRECTORY* ','start')
        }
    whenever sb->scroll do                  # **
        lb->view($value)                    # **
    whenever lb->yscroll do                 # **
        sb->view($value)                    # **
In this example, first note that the lines tagged with ** are nearly identical to the listbox plus scrollbar example in § 10.4.2. The remaining lines setup a button and an entry widget. Next the listbox plus scrollbar dialog from the previous example is used. In this dialog, the user will enter a directory in the entry widget, and the contents of the directory will show up in the listbox (see Figure 10.10).

The mechanics of this are controlled by the first whenever. Whenever the user presses the button or hits the <return> key in the entry widget, the listbox is cleared, the string from the entry widget is retrieved, an ls is done on the directory, and if it succeeds, the output is used to fill the listbox otherwise an error is indicated in the entry widget. The complicated shell() command which actually does the ls just checks to see if the directory exists, and if it does exist, the ls is done, otherwise the shell command exits with a non-zero status.

  figure7114
Figure 10.10: Entry

This example shows three of the entry widget events, get, insert, and return. get simply returns the contents of the entry widget. insert is used to insert text into the entry widget. The second parameter to insert is optional, and it specifies where to insert the string. By default, the string is inserted at the end. The return event is generated when the user hits the <return> key in the entry widget. Table 10.14 list all of the parameters to entry(), and Table 10.15 lists the entry widget events.

 

Parameter Default Values Description
parent widget parent of the entry
width 0 integer width 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?
show T boolean show typed characters?
exportselection F boolean export to X clipboard?
fill 'x 'x' 'y' 'both' 'none' how to expand when resized
Table 10.14: Entry Construction Parameters

   

 

Event I/O Values Description
backgroundtex2html_wrap_inline15708 X color change background color
bind tex2html_wrap_inline15708<X> <G> associate Xevent <X> with Glish event <G>
borderwidthtex2html_wrap_inline15708 dimension change border width
delete tex2html_wrap_inline15708string delete indicated character, use 2 strings to delete range
disabletex2html_wrap_inline15708 disable widget, must be balanced by enable
disabled tex2html_wrap_inline15708 boolean set state, disable (T) or enable (F)
enabletex2html_wrap_inline15708 enable widget, must be balanced by disable
exportselection tex2html_wrap_inline15708boolean export selection to X clipboard?
font tex2html_wrap_inline15708 X font change text font
foregroundtex2html_wrap_inline15708 X color change foreground color
get tex2html_wrap_inline15712 get current contents
insert tex2html_wrap_inline15708string insert item, optional 2nd param insert index
justify tex2html_wrap_inline15708 'center' 'left' 'right' set justification for text
relief tex2html_wrap_inline15708 'flat' 'ridge' 'raised' 'sunken' 'groove' change border relief
return tex2html_wrap_inline15710string generated when user hits return, returns contents
show tex2html_wrap_inline15708boolean show typed characters?
view tex2html_wrap_inline15708record scrollbar update event
width tex2html_wrap_inline15708integer entry width in character units
xscroll tex2html_wrap_inline15710double information for horizontal scrollbar update
Table 10.15: Entry Events

   

Notice in Table 10.15 that the entry widget generates the events necessary to connect a horizontal scrollbar to the entry widget. This is done in a way analogous to connecting a vertical scrollbar to the listbox, i.e. change yscroll to xscroll.  


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

Thu Nov 13 16:44:05 EST 1997