Special Widgets

Most widgets, such as buttons, labels, and entries, are simple and straightforward. However, some are more complex and deserve more attention.

Scrollbars

Say that you want to create a list of options. You can do this using the ListBox widget. But you may have more items than can be displayed at one time.

No problem, just create a Scrollbar widget. Now all you need to do is connect the movement callbacks from the scrollbar to your ListBox widget.

Coding this operation is both time consuming and tricky, so the makers of Tk created a way of automatically adding scrollbars to a widget through the Scrolled method.

For example, if you wanted to create a ListBox 10 lines high and 30 characters wide, you would use the command

$tk_mw–>Listbox(
    –height => 10, 
    –width => 30 
);

If you wanted to create one with a vertical scrollbar, you would use the command

$tk_mw–>Scrolled(
    'Listbox', 
    –scrollbars => "eo", 
    –height => 10, 
    –width => 30 
)–>pack(
    –side => "top", 
    –expand => "yes", 
    –fill => "both" 
);

The first argument to the Scrolled method is the widget to be created. The option –scrollbars tells the method where the scrollbars are to be created. It contains one or more directions (n, s, e, w).

Each direction can be followed by the letter o. This tells Tk that the scrollbar is to be displayed only if needed. Without the o, the scrollbar will always be displayed. So for example, the option

–scrollbar => "eos"

causes a scrollbar to be displayed on the east (right side) of the widget if needed, and another scrollbar is placed on the south (bottom) of the widget. The bottom scrollbar will always be present.

Text

The Text widget contains multiline text—no surprise there. It also lets you place things other than text, such as images and widgets.

Text tags are used to specify a portion of the text. For example, suppose that you want to highlight some text. One way of doing this is to change the background to yellow. The first thing you need to do is create a tag that defines how highlighted text is supposed to look. Naturally, this tag is called 'highlight', and it is created with the code

$text_box–>tagConfigure('highlight', background => 'yellow'),

In this example, $text_box is a text widget. The tagConfigure function is used to create or change a tag. The name of the tag is 'highlight'. We could have chosen any name for this tag, but 'highlight' seemed to be the easiest to remember. This is followed by the list of attributes for this tag. In this case, there’s only one attribute, background, and you want it to be yellow.

Now that you have defined the tag, apply it to the first two lines of the text. This is done by adding the tag to the text:

$text_box–>tagAdd('highlight', 
    "0.0", 
    "1.0 lineend");

This example changes the first line (numbered 0 and 1) and adds the tag 'highlight'. This turns the background yellow (in addition to any other tags that apply to this text).

You start at the beginning of the first line—that’s line 0, character 0, or "0.0". You end at the end of the second line. This is specified as "1.0 lineend", or in Tk terms, line 1, character 0, and then go to the end of line (lineend).

Tags give you great flexibility in displaying text. You also can bind events (see the following section, “Events”) to tags, which allows them to respond to events such as mouse clicks, function keys, focus changes, and other events.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset