|
|
|
|
|
After the text widget is created, there are several different ways to place text in it. The user can type directly into it, or you can use the insert method: |
|
|
|
 |
|
|
|
|
$text->insert('end', "To be or not to be...\nThat is the question"); |
|
|
|
|
|
|
|
|
The basic form of the insert method takes two arguments. The first is an index value that indicates where to start placing the text. The second argument is the string to insert. Unlike the listbox insert method, You can't use an array as the second argument. If you do, only the first item in the array is inserted into the text box. |
|
|
|
|
|
|
|
|
A typical use of the text widget is to read a file and place it in the text widget as it's read: |
|
|
|
 |
|
|
|
|
$text = $mw->Scrolled("Text")->pack();
open (FH, "chapter1") || die "Could not open chapter1";
while (<FH>) {
$text->insert ('end', $_);
}
close(FH); |
|
|
|
|
|
|
|
|
You can use the text widget to display the file backward (line by line) by changing the insert line to $text->insert (0, $_). This will put the next line read at the top of the text widget instead of at the end. |
|
|
|
|
|
|
|
|
The text widget can do a lot more than just display a file or two lines from a Shakespearean play. In addition to options, we also have tags, indexes, and marks to control how the contents of a text widget are displayed. |
|
|
|
|
|
|
|
|
Options used with the Text method change the way the text is displayed within the text widgets. The following options are standard for all the widgets: -background, -borderwidth, -cursor, -exportselection, -foreground, -highlightbackground, -highlightcolor, -highlightthickness, -insert-background, -insertborderwidth, -insertofftime, -insertontime, -insertwidth, -padx, -pady, -selectbackground, -selectborderwidth, -selectforeground, -setgrid, -state, -takefocus, -wrap, -xscrollcommand, and -yscrollcommand. |
|
|
|
|
|
|
|
|
To find out more about what these options do, check back to Chapter 3, The Basic Button, where they were first covered. |
|
|
|
 |
|
|
|
|
-background => color
Changes the color of the screen displayed behind the text. |
|
|
|
 |
|
|
|
|
-borderwidth=> amount
Sets the width of the edges of the widget. |
|
|
|
 |
|
|
|
|
-cursor => cursorname
Sets the cursor displayed when the mouse cursor is in front of the text widget. |
|
|
|
|