Embedded Widgets

   

Practical Programming in Tcl & Tk, Third Edition
By Brent B. Welch

Table of Contents
Chapter 33.  The Text Widget


The text widget can display embedded widgets as well as text. You can include a picture, for example, by constructing it in a canvas and then inserting the canvas into the text widget. An embedded widget takes up one character in terms of indices. You can address the widget by its index position or by the Tk pathname of the widget.

For example, suppose $t names a text widget. The following commands create a button and insert it into the text widget. The button behaves normally, and in this case it invokes the Help command when the user clicks on it:

 button $t.help -bitmap questhead -command Help $t window create end -window $t.help 

By default an embedded widget is centered vertically on its text line. You can adjust this with the -align option to the window create command. This setting only takes effect if the window is smaller than the text in the line. I find that windows are usually larger than the text line, and in that case the -align setting has no effect. This setting is also used with images, however, where it is more common to have small images (e.g., for special bullets). Table 33-5 describes the window and image alignment settings:

Table 33-5. Window and image alignment options.
topTop lines up with top of text line.
centerCenter lines up with center of text line.
baselineBottom lines up with text baseline.
bottomBottom lines up with bottom of text line.

You can postpone the creation of the embedded widget by specifying a Tcl command that creates the window, instead of specifying the -window option. The delayed creation is useful if you have lots of widgets embedded in your text. In this case the Tcl command is evaluated just before the text widget needs to display the widget. In other words, when the user scrolls the text so the widget will appear, the Tcl command is run to create the widget:

Example 33-4 Delayed creation of embedded widgets.
 $t window create end -create [list MakeGoBack $t] proc MakeGoBack { t } {    button $t.goback -text "Go to Line 1" \       -command [list $t see 1.0] } 

The MakeGoBack procedure is introduced to eliminate potential quoting problems. If you need to execute more than one Tcl command to create the widget or if the embedded button has a complex command, the quoting can quickly get out of hand.

Table 33-6 gives the complete set of options for creating embedded widgets. You can change these later with the window configure operation. For example:

 $t window configure $t.goback -padx 2 

Table 33-6. Options to the window create operation.
-align whereAlignment: top, center, bottom, or baseline.
-create commandTcl command to create the widget.
-padx pixelsPadding on either side of the widget.
-pady pixelsPadding above and below the widget.
-stretch booleanIf true, the widget is stretched vertically to match the spacing of the text line.
-window pathnameTk pathname of the widget to embed.

You can specify the window to reconfigure by its pathname or the index where the window is located. In practice, naming the widget by its pathname is much more useful. Note that end is not useful for identifying an embedded window because the text widget treats end specially. You can insert a window at end, but end is always updated to be after the last item in the widget. Thus end will never name the position of an existing window.


       
    Top
     



    Practical Programming in Tcl and Tk
    Practical Programming in Tcl and Tk (4th Edition)
    ISBN: 0130385603
    EAN: 2147483647
    Year: 1999
    Pages: 478

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net