Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
The text widget is used to display large amounts of textural information. The 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 looks like the one shown in Figure 11.11.
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 above example
is an example of an index passed with the insert event.
Indexes are also used to tag regions of the text widget. For example if you want to change the background of the string ``this line is'' to red, you 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 is set here. Multiple attributes can be passed to config, and the configuration of a tag done before any section of the text widget has been tagged. Tags can also be set as lines 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 is used to delete a tag. Deleting a tag removes any configuation changes.
You can edit and modify the text in the text widget. The text widget can also be disabled; this prevents text from being added by the user. 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 11.16 lists all of the parameters available for the text widget.
In the example above, wrap is important because if you specified the lines should wrap, the default behavior, there would be no need for a horizontal scrollbar.
Table 11.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.
|