next up previous contents index
Next: Canvas Up: Glish/Tk Previous: Odds and Ends


  The Tk text widget is a complex widget. In time, the user should be able to write a full blown editor using the text widget, but even though the text widget is not completely integrated into Glish yet, it is still quite usable.

The text widget is used to display large amounts of textural information. xscroll, yscroll, and view events are available to connect vertical and horizontal scrollbars to the text widget. Here is an example of the creation of a text widget:

    f := frame()
    tf := frame(f,side='left',borderwidth=0)
    t := text(tf,relief='sunken',wrap='none')
    vsb := scrollbar(tf)
    bf := frame(f,side='right',borderwidth=0)
    pad := frame(bf,expand='none',width=23,height=23,relief='groove')
    hsb := scrollbar(bf,orient='horizontal')
    whenever vsb->scroll, hsb->scroll do
    whenever t->yscroll do
    whenever t->xscroll do
    t->insert(' this line is a very very long line','8.end')
In this example, a text widget is created along with two scrollbars. A frame, pad, is used to pad out the horizontal scrollbar so that it doesn't run past the end of the text widget. Next, the scroll events between the text widget and the scrollbar are connected up. Finally, some initial text is added to the text widget. This simple dialog would look like the one shown in Figure 10.11.

Figure 10.11: Text

One thing to note is that indexes for the text widget are written as line.position where line is the line number, and position is the character position within the line. The last line of the example above has an example of an index passed with the insert event.

Indexes can also be used to tag regions of the text widget. For example if we wished to change the background of the string ``this line is'' to red, we would do it by first taging the section and then changing the configuration for that tag:

    t->addtag( 'red', '8.6', '8.18' )
    t->config( 'red', background='red' )
All of the standard Tk attributes can be set in the same way background was set here. Multiple attributes can be passed to config, and the configuation of a tag can be done before any section of the text widget has been tagged. Tags can also be set as lines are inserted into the text widget:
    t->append( '\nthirteen', 'red' )
    t->insert( 'this is line ', '13.0', 'red' )
Here, the string ``this is line thirteen'' is appended to the text widget, and the whole line has a red background. The deltag event can be used to delete a tag. Deleting a tag removes any configuation changes.

The user can edit and modify the text in the text widget. The text widget can be disabled, but this prevents text from being added either by the user or from Glish. So in general for smaller amounts of non-editable text, it is probably better to use the message widget. For things like listing a long copyright notice, e.g. the GNU GPL, a disabled text widget might be a good choice.

Table 10.16 lists all of the parameters available for the text widget.


Parameter Default Values Description
parent widget parent
width 0 integer width in character units
height 1 integer height in lines
wrap 'word' 'none' 'char' 'word' line wrap behavior
font '' X font font of text
disabled F boolean is inactivated?
text '' string initial text
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 'both' 'x' 'y' 'both' 'none' how to expand when resized
Table 10.16: Text Construction Parameters


In the example above, wrap was important because if we had specified that the lines should wrap, the default, then there would be no need for a horizontal scrollbar.

Table 10.17 lists all of the events which are associated with the text widget. The format of text widget indexes, as shown above, is important for several of the events.


Event I/O Values Description
addtag tex2html_wrap_inline15708strings create tag (1st arg), 2nd and 3rd args are indexes
append tex2html_wrap_inline15708string insert string at end, opt n params indicate tags
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
config tex2html_wrap_inline15708<T>, <A>& change attributes \verb<A> of text tagged <T>
delete tex2html_wrap_inline15708string use one index to delete a character, use two to delete a range
deltag tex2html_wrap_inline15708string delete tag (1st arg)
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
font tex2html_wrap_inline15708 X font change text font
foregroundtex2html_wrap_inline15708 X color change foreground color
get tex2html_wrap_inline15712string use one index to get a character, use two to get a range
height tex2html_wrap_inline15708integer height in lines
insert tex2html_wrap_inline15708string insert string, 2nd param is the insert index, opt n params indicate tags
prepend tex2html_wrap_inline15708string insert string at start, opt n params indicate tags
relief tex2html_wrap_inline15708 'flat' 'ridge' 'raised' 'sunken' 'groove' change border relief
see tex2html_wrap_inline15708string index indicates a position to scroll to
view tex2html_wrap_inline15708record scrollbar update event
width tex2html_wrap_inline15708integer width in character units
wrap tex2html_wrap_inline15708 'none' 'char' 'word' change line wrap behavior
xscroll tex2html_wrap_inline15710double information for horizontal scrollbar update
yscroll tex2html_wrap_inline15710double information for vertical scrollbar update
Table 10.17: Text Events



next up previous contents index
Next: Canvas Up: Glish/Tk Previous: Odds and Ends

Thu Nov 13 16:44:05 EST 1997