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


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


Text

The Tk text widget is a complex widget. You can write a full blown editor using the text widget, even though the text widget is not yet completely integrated into Glish.

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.

Figure 11.11: Text
\begin{figure}
\centerline{\psfig{figure=tktext.eps,scale={0.7}}}\end{figure}

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.


Table 11.16: Text Construction Parameters
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
hlcolor '' X color highlight color with focus
hlbackground '' X color highlight color without focus
hlthickness [] dimension hightlight border thickness

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.


Table 11.17: Text Events
Event I/O Values Description
addtag $ \rightarrow$ strings create tag (1st arg), 2nd and 3rd args are indexes
append $ \rightarrow$ string insert string at end, opt n params indicate tags
background $ \rightarrow$ X color change background color
bind $ \rightarrow$ <X> <G> associate Xevent <X> with Glish event <G>
borderwidth $ \rightarrow$ dimension change border width
config $ \rightarrow$ <T>, <A> change attributes <A> of text tagged <T>
delete $ \rightarrow$ string use one index to delete a character, use two to delete a range
deltag $ \rightarrow$ string delete tag (1st arg)
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 change text font
foreground $ \rightarrow$ X color change foreground color
get $ \Leftrightarrow$ string use one index to get a character, use two to get a range
height $ \rightarrow$ integer height in lines
hlbackground $ \rightarrow$ X color highlight color without focus
hlcolor $ \rightarrow$ X color highlight color with focus
hlthickness $ \rightarrow$ dimension hightlight border thickness
insert $ \rightarrow$ string insert string, 2nd param is the insert index, opt n params indicate tags
prepend $ \rightarrow$ string insert string at start, opt n params indicate tags
ranges $ \Leftrightarrow$ string get ranges for given tag
relief $ \rightarrow$ 'flat' 'ridge' 'raised' 'sunken' 'groove' change border relief
see $ \rightarrow$ string index indicates a position to scroll to
view $ \rightarrow$ record scrollbar update event
width $ \rightarrow$ integer width in character units
wrap $ \rightarrow$ 'none' 'char' 'word' change line wrap behavior
xscroll $ \leftarrow$ double information for horizontal scrollbar update
yscroll $ \leftarrow$ double information for vertical scrollbar update


next up previous contents index
Next: Canvas Up: Glish/Tk Previous: Odds and Ends   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