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 t->view($value) whenever t->yscroll do vsb->view($value) whenever t->xscroll do hsb->view($value) t->append('one\ntwo\nthree\nfour\nfive\nsix\n') t->append('seven\neight\nnine\nten\neleven\ntwelve') 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
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 strings create tag (1st arg), 2nd and 3rd args are indexes append string insert string at end, opt n params indicate tags background X color change background color bind
<X> with Glish event
borderwidth dimension change border width config
<A>& change attributes \verb<A> of text tagged
delete string use one index to delete a character, use two to delete a range deltag string delete tag (1st arg) disable disable widget, must be balanced by enable disabled boolean set state, disable (T) or enable (F) enable enable widget, must be balanced by disable font X font change text font foreground X color change foreground color get string use one index to get a character, use two to get a range height integer height in lines insert string insert string, 2nd param is the insert index, opt n params indicate tags prepend string insert string at start, opt n params indicate tags relief 'flat' 'ridge' 'raised' 'sunken' 'groove' change border relief see string index indicates a position to scroll to view record scrollbar update event width integer width in character units wrap 'none' 'char' 'word' change line wrap behavior xscroll double information for horizontal scrollbar update yscroll double information for vertical scrollbar update