Next: Configuring and Deleting Up: Creating Display Items Previous: Creating Display Items


Setting the Styles of the Display Items 

Note that in the above example, if we want to control the foreground color of the text items, we cannot issue commands such as:

.t insert end -itemtype text -text "First Item" -foreground black

because -foreground is not an individual attribute of the text item. Instead, it is a collective attribute and must be accessed using a display style object. To do that we can use the command tixItemStyle to create display styles, as shown in the following example:

set style1 [tixDisplayStyle text -font 8x13]
set style2 [tixDisplayStyle text -font 8x13bold]

tixTList .t; pack .t

.t insert end -itemtype text -text "First Item" -underline 0
-style $style1
.t insert end -itemtype text -text "Second Item" -underline 0
-style $style2
.t insert end -itemtype text -text "Third Item" -underline 0
-style $style1

The first argument of tixDisplayStyle specify the type of style we want to create. Each type of display item needs its own type of display styles. Therefore, for example, we cannot create a style of type text and assign it to an item of type image. The subsequent arguments to tixDisplayStyle set the configuration options of the collective attributes defined by this style. A complete list of the configuration options of each type of the display style is in figures ??? through ???.

The tixDisplayStyle command returns the names of the newly created styles to us and we use the variables style1 and style2 to store these names. We can then assign the styles to the display items by using the names of the styles. As shown in figure 3-5, by assing these two styles to the -style option of the display items, we assigned a medium-weight font to the first and third item and a bold font to the second item.

(Figure 3-5) Two Display Styles With Different Fonts 

The name of the style returned by tixDisplayStyle is also the name of a command which we can use to control the style. For example, we can use the following commands to switch the fonts in the two styles we created in the above example:

$style1 configure -font 8x13bold
$style2 configure -font 8x13

After the execution of the above command, the font in the second item in the TixTList widget becomes medium-weight and the font in the first and third items becomes bold, as shown in figure 3-5.