Navigation Logo 12.4  Colors Navigation Logo

 

 

Colors are represented as three hexadecimal numbers that determine their red, green, and blue parts. Each of these three numbers can be up to four hexadecimal digits. You can specify fewer digits if you are consistent and do it for all three numbers. When you specify fewer the number is padded with 0s on the right. A color specification consists of the character "#" followed by these numbers in the order: red, green, blue. For example, #00ff00 is a specification for pure green.

Using hexadecimal numbers to identify colors is not much fun. Figure 12.4a shows a window displayed by a script designed to help. Move the sliders in this window to change the red, green, and blue components of the color patch on the left. A color specification for that color appears below. This script appears below in Scale where there is an explanation of how it works. It is also available on line as Script S15.11a.

Figure 12.4a: Choosing a color interactively.

Here is a list of color options that can be used with most built-in widget types:

-background
The widget's background color. This option is sometimes written -bg.

-foreground
The widget's foreground color. (Frame widgets have no foreground color.) This option is sometimes written -fg.

-highlightcolor
The widget's highlight color is the color given to a small highlight border around the edge whenever the widget has the focus.

-highlightbackground
The widget's highlight background color is the color given to a small highlight border around the edge whenever the widget does not have the focus.

-highlightthickness
Not exactly a color option. Use this option to control the width of the small highlight border. A value of 0 means there will be no visible color change when the widget's focus status changes. The default value of this option is 0 for widgets whose default configuration has no use for the focus. When specifying a nonzero value, you are specifying a screen distance.

Except for -highlightthickness, all these options require a color specification as a value. The numeric specification described above will always work. Often, you can get away with an ordinary color name such as white, black, red, green, blue, and so forth.

There are many other color-related options that are widget specific. Your on-line manual and a little experimentation are the best sources of information about new or unusual widgets.

Exercise 12.4a

Create and pack a square frame with a white background.

Solution

Exercise 12.4b

Finish the following loop to test several possible color names to see if Tk recognizes them.
foreach C LIST_OF_COLOR_NAMES { 
   ... 
}
Each color name should create a square frame of that color or a label whose text is an unusable color name.

Solution

Exercise 12.4c

Finish implementing this procedure
proc box {Parent Color {Shape ""}} { ... }
so that it creates a rectangle of the given Color and with the given Parent widget. If the Shape argument is empty the rectangle should be a square with one centimeter sides.

The Shape argument can have two nonempty values "h" and "v." When Shape is "h," your rectangle should be 3 centimeters long and 1 centimeter high. When Shape is "v," your rectangle should be 1 centimeter long and 3 centimeters high.

The box procedure must generate a unique name for the widget it creates. Like Tk's built-in widget makers, it should return that name as it finishes. Here is an example use of box:

% box . red
.red1
% pack .red1

Solution

 

 

[Sample TK Application]
Author's Home Page
Navigation Logo [Book's Cover]
Order from Amazon.