An Introduction to Tkinter
  Copyright © 1997 by Fredrik Lundh <[email protected]>  
  Updated 10 Aug 1997  

< Widget Configuration | Option Values | Events and Bindings >

Option Values

Distances

Coming soon.

Colors

Tk allows you to specify colors in two different ways. You can either specify a color name, or explicitly specify the red, green, and blue color components.

Color Names

Available color names include common names like Red, Green, Blue, Yellow, and LightBlue, but also more exotic things like Moccasin, PeachPuff, etc.

On an X window system, the color names are defined by the X server. You might be able to locate a file named xrgb.txt which contains a list of color names and the corresponding RGB values. On Windows and Macintosh systems, the color name table is built into Tk.

Under Windows, you can also use the Windows system colors (these can be set by the user):

SystemActiveBorder, SystemActiveCaption, SystemAppWorkspace, SystemBackground, SystemButtonFace, SystemButtonHighlight, SystemButtonShadow, SystemButtonText, SystemCaptionText, SystemDisabledText, SystemHighlight, SystemHighlightText, SystemInactiveBorder, SystemInactiveCaption, SystemInactiveCaptionText, SystemMenu, SystemMenuText, SystemScrollbar, SystemWindow, SystemWindowFrame, and SystemWindowText.

On the Macintosh, the following system colors are available:

SystemButtonFace, SystemButtonFrame, SystemButtonText, SystemHighlight, SystemHighlightText, SystemMenu, SystemMenuActive, SystemMenuActiveText, SystemMenuDisabled, SystemMenuText, and SystemWindowBody.

Color names are case insensitive. Many (but not all) color names are also available with or without spaces between the words. For example, "lightblue", "light blue", and "Light Blue" all specify the same color.

RGB Specifications

If you need to explicitly specify a color, you can use a string with the following format:

#RRGGBB

RR, GG, BB are hexadecimal representations of the red, green and blue values, respectively. The following sample shows how you can convert a color 3-tuple to a Tk color specification:

    tk_rgb = "#%02d%02d%02d" % (128, 192, 200)

Tk also supports the forms "#RGB" and "#RRRRGGGGBBBB" to specify each value with 16 and 65536 levels, respectively.

You can use the widget method winfo_rgb to translate a color string (either a name or an RGB specification) to a 3-tuple:

    rgb = widget.winfo_rgb("red")
    red, green, blue = rgb[0]/256, rgb[1]/256, rgb[2]/256

Note that winfo_rgb returns 16-bit RGB values, ranging from 0 to 65535. To map them into the more common 0-255 range, you must divide each value by 256 (or shift them 8 bits to the right).

Fonts

Tk allows you to specify fonts in a variety of ways. With any Tk versions before 8.0, you can only use X font descriptors. Such a descriptor is a string having the following format (the asterisks represent fields that are usually not relevant, and which are not described in this summary):

"-*-family-weight-slant-*--*-size-*-*-*-*-charset"

The font family is typically something like Times, Helvetica, Courier or Symbol.

The weight is either Bold or Normal. Slant is either R for "roman" (normal), I for italic, or O for oblique (in practice, this is just another word for italic).

Size is the height of the font in decipoints (that is, points multiplied by 10). There's usually 72 points per inch, but some low-resolution displays may use larger "logical" points to make sure that small fonts are still legible. The character set, finally, is usually ISO8859-1 (ISO Latin 1), but may have other values for some fonts. The following descriptor requests a 12-point boldface Times font, using the ISO Latin 1 character set:

"-*-Times-Bold-R-*--*-120-*-*-*-*-ISO8859-1"

If you don't care about the character set, or use a font like Symbol which has a special character set, you can use a single asterisk as the last component:

"-*-Symbol-*-*-*--*-80-*"

A typical X server supports at least Times, Helvetica, Courier, and a few more fonts, in sizes like 8, 10, 12, 14, 18, and 24 points, and in normal, bold, and italic/oblique variants. Most servers also support scaleable fonts. You can use programs like xlsfonts and xfontsel to check which fonts you have access to on a given server.

This kind of font descriptors can also be used on Windows and Macintosh. If you use Tk 4.2, you should keep in mind that the font family must be one supported by Windows. Here are some families available on most Windows platforms:

Arial (corresponds to Helvetica), Courier New (Courier), Fixedsys, MS Sans Serif, MS Serif, Symbol, System, Times New Roman (Times), and Wingdings:

Tk 8.0 automatically maps Courier, Helvetica, and Times to their corresponding native family names on all platforms. In addition, a font specification can never fail under Tk 8.0 -- if Tk cannot come up with an exact match, it tries to find a similar font. If that fails, Tk falls back to a platform-specific default font. Tk's idea of what is "similar" probably doesn't correspond to your own view, so you shouldn't rely too much on this feature.

System fonts

In addition to the X specific font descriptors, Tk also supports system specific font names. Under X, these are usually font aliases like fixed, 6x10, etc. Under Windows, these include ansi, ansifixed, device, oemfixed, system, and systemfixed:

Under Macintosh, the system font names are application and system.

Note that the system fonts are full font names, not family names, and they cannot be combined with size or style attributes. For portability reasons, avoid using these names wherever possible.

Font descriptors

Tk 8.0 also supports platform independent font descriptors. Under Tk 8.0, you can specify a font as tuple containing a family name, a height in points, and optionally a string with one or more styles. Examples:

("Times", 10, "bold")

("Helvetica", 10, "bold italic")

("Symbol", 8)

The available styles are normal, bold, roman, italic, underline, and overstrike. Again, the family names Courier, Helvetica, and Times are guaranteed to work on all platforms.

Tk 4.2 under Windows actually supports this kind of font descriptors as well. There are several restrictions, including that the family name must exist on the platform, and not all the above style names exist (or rather, some of them have different names).

Font names

In addition, Tk 8.0 allows you to create named fonts and use their names when specifying fonts to the widgets. If you change a named font, the changes are automatically propagated to all widgets using the font. Tkinter currently doesn't support the creation of named fonts, so we won't discuss this yet.

Cursors

Coming soon.

< Widget Configuration | Option Values | Events and Bindings >