home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeMastering Perl/TkSearch this book

Chapter 18. A Tk Interface Extension Tour

Tk Interface Extension (Tix) widgets are an additional set of widgets that come with the Tk module.

In this chapter, we cover the widgets from Ioi Lam's Tix 4.1.0 package. TList, HList, Tree, and DirTree are container-style widgets, designed to hold display items. This chapter covers:

A much more flexible Listbox that uses display items.

A hierarchical list widget, often used as a snazzy Listbox. It is the base class for Tree and DirTree.

Based on HList, it displays items in tree format. Look at DirTree for an easy way to work with filesystems .

An extension of Tree, DirTree is designed to show directories and files in a hierarchical format.

You'd use the TList widget in place of a a ListBox when you want to display something other than text or want to display individual items in different fonts or colors. HList gives you the ability to structure a list hierarchically or with elegant column headings. The Tree widget provides a simpler interface for creating an HList with indicators, and DirTree is further simplified for displaying a filesystem hierarchy.

Before we talk about the widgets individually, we need to give you some background on how display items work and how to configure them. Once that's established, we can discuss the Tix widgets in detail.

18.1. Display Items

One of the things we gain when using Tix widgets is the ability to create a display item and then add it to a Tix widget for display. Display items are all rectangular, and the container widget manipulates those rectangles with little regard to anything other than the size of the rectangles and the order in which to display them.

There are four different types of items you can use in Tix widgets: text, imagetext, image, and window. A text item displays only text. An imagetext item can display both an image and text, or only an image, or only text. An image item displays only an image, and a window is another widget.

When creating each item in our TList, we specify the type using the -itemtype option:

$tl = $mw->TList->pack(-expand => 1, -fill => 'both');
foreach my $i (0..19) { 
  $tl->insert('end', -itemtype => 'text', -text => "Display Item #$i");

In Figure 18-1, we see there are 20 different display items inserted. Each has an item type of 'text'.

Figure 18-1

Figure 18-1. A TList widget showing display items

Now let's change our example to show different item types:

$tl = $mw->TList->pack(-expand => 1, -fill => 'both');
my $image = $mw->Getimage('folder');

foreach my $i (0..4) { 
  $tl->insert('end', -itemtype => 'text', -text => "text Item #$i");
  $tl->insert('end', -itemtype => 'imagetext', 
              -text => "imagetext item #$i", -image => $image);
  my $b = $tl->Button(-text => "Window item #$i", 
                      -command => sub { print "Button pressed\n"; });
  $tl->insert('end', -itemtype => 'window', -widget => $b);

Figure 18-2 shows our screenshot, generated using the previous code, then manually resized to make it pretty with three items per column.

Figure 18-2

Figure 18-2. A TList widget showing different display item types

We didn't bother showing the 'image' type in this example, since it's just an image with no text on it. The fonts and colors used to display each item are the defaults for the TList widget, but we could change them on a per item basis using item styles (covered in the next section).

Table 18-1 lists the options you can use when creating each of the display item types. Different widgets use different methods to create item types. In our previous examples, we used TList, so the method to create a new item is insert. If we were using the HList widget, we would call itemCreate to create a new item.

Table 18-1. The options available for item types in Tix widgets







-bitmap => bitmap
A bitmap to display in the item.


Figure 18-1

Figure 18-1

-image => image
An image to display in the item.


Figure 18-1

Figure 18-1

-showimage => 0 | 1
Determines whether the image will be shown. Even if the image isn't being displayed, space will still be allocated for it.


Figure 18-1

-showtext => 0 | 1
Determines whether the text will be shown.


Figure 18-1

-style => $style
A reference to a style created using the ItemStyle method. Will change how the item is displayed: font, colors, etc.

Figure 18-1

Figure 18-1

Figure 18-1

Figure 18-1

-text => string
The text to be displayed. If using -showtext => 0 on an imagetext item, the text won't be seen.

Figure 18-1

Figure 18-1

-underline => index
The character index to underline in the text. First character in the text is at 0.

Figure 18-1

Figure 18-1

-window => $widget
The widget to display.


Figure 18-1

-widget => $widget
An alias for the -window option. You can use either -window or -widget as the option name.


Figure 18-1

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.