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

Book HomeMastering Perl/TkSearch this book

4.15. Color Options

The options that can change the Button's colors are -background,-foreground,-activebackground,-activeforeground,and-disabledforeground. Each option takes a string that identifies a color. This string could be either a color description such as "blue" or a hex string such as "#d9d9d9", which also describes a color but is much more cryptic.

For either Win32 or Unix systems, you can run the widget demo included with the Tk module. If the perl/bin directory is in your path, you can simply type "widget"on the DOS or Unix command line. Under the Listbox section in the demo is an example that displays color names. You can double-click the names in the list to see them change the application's color.

Valid values for the color string are available on your Unix system in a file called rgb.txt. This file is typically located in the X11 lib directory, for example, /usr/X11R6/lib/rgb.txt. You can also use the X application xcolors or showrgb. Check the manpages for each command to determine the best way to use it.

Another place to look for valid color names (and this applies to Win32 as well) is in your Perl distribution directory. Look for the file xcolors.h. It is a text file that contains the RGB values and names for quite a few colors. On a Win32 machine, it might be in C:\Perl\lib\site\Tk\ptk.

The color of the Button depends on the Button's state. When the Button has a state of 'normal', the colors assigned to -foreground and -background are in effect. The background of the Button is the area behind the text string but within the edges of the Button.

The background is specified like this:

-background => color

The default background color is light gray. Figure 4-32 shows the results of changing the second Exit Button's background to blue.

Figure 4-32

Figure 4-32. Example of -background => 'blue'

The foreground of the Button is the text (or bitmap). The foreground color is specified like this:

-foreground => color

By default, -foreground is 'black'. Make sure whatever color you pick contrasts enough with the background color to be readable. In the example in Figure 4-32, we left the text the default color, which doesn't contrast well with the background color of the Button. If we change -foreground to 'white', we will be able to see the text much more easily, as you'll see in Figure 4-33. (The shortcut for -foreground is -fg,which may or may not work on other types of widgets. For total compatibility, always stick with -foreground as the option name.)

Figure 4-33

Figure 4-33. Example of -background => 'blue' and -foreground => 'white'

When you use the -foreground and -background options with a bitmap, the bitmap foreground and background will change to the specified colors. The effect of the colors depends on the bitmap. See Figure 4-34.

Figure 4-34

Figure 4-34. 'error' bitmap with -foreground => 'white' and -background => 'black'

The -foreground and -background options control what color the Button is in the 'normal' state. When the Button has the mouse cursor over it, the -activebackground and -activeforeground colors are used:

-activebackground => color, -activeforeground => color

These colors are different because we want users to have some visual clues that they can press the Button. By having the colors change slightly when the mouse cursor is over the Button, users know that the Button can be pressed to do something. The default for -activebackground is a slightly darker gray color ("#ececec").

The final color option, -disabledforeground, is the color of the text when the Button's state is 'disabled'.

-disabledforeground => color

When the Button is in a disabled state, it will not respond when the mouse cursor is over it or if it is pressed. The default text color (or bitmap color) is "#a3a3a3". Figure 4-35 shows the difference between the text colors of one disabled Button and one normal Button. (We also saw this example in Figure 4-15. Look there for the code that created this window.)

Figure 4-35

Figure 4-35. -disabledforeground example

Library Navigation Links

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