Fonts, Colors, Images
Fonts, Colors, Images
This chapter describes how Tk handles fonts, colors and images. We've touched on all of these before, but here we'll provide a more in depth treatment.
Fonts
Several Tk widgets, such as the label, text, and canvas, allow you to specify the fonts used to display text, typically via a "font" configuration option. As with many things in Tk, the default fonts are usually a good choice, but if you do want to make changes, this section will describe several ways to do so. onts are one of several areas that are highly platform specific, so how you specify them is important. The font command reference provides full details on specifying fonts, as well as other font!related operations. "ost of the themed widgets that display text don't have a
"font" configuration
option, unlike the classic Tk widgets. #ather than modifying individual widgets, the correct approach in the themed widgets is to specify the fonts used in a style, and then use that style for the individual widget. This is akin to the difference between hardcoding display!oriented markup like font tags inside $T"% pages, vs. using &SS stylesheets that keep all the display specific information in one place. Many older Tk programs hardcoded a lot of fonts, using either the "family size style" format we'll see below, X11 font names, or the older and more arcane X11 font specification string. n many cases, this left these applications with a dated look as platforms e!ol!ed. "urther, many programs specified fonts on a per#widget basis, lea!ing the font decisions spread out through the program. $amed fonts, and use of the standard fonts that Tk pro!ides, are a far better solution. %e!iewing and updating the usage of fonts is an easy and important change to make in any e&isting applications.
Standard Fonts
'articularly for more!or!less standard user interface elements, each platform defines specific fonts that should be used. Tk encapsulates many of these into a standard set of fonts that are always available, and of course the standard widgets use these fonts. This helps abstract away platform differences. The predefined fonts are(
The default for all )*+ items not otherwise specified. *sed for entry widgets, listboxes, etc. A standard fixed!width font. The font used for menu items. The font typically used for column headings in lists and tables. A font for window and dialog caption bars. dialogs
Named Fonts
3ou can also create your own fonts, which can be used exactly like the predefined ones. To do so, you'll need to pick a name to refer to the font, and then specify various font attributes that define how the font should look. Typically, you'd use different font attributes on different platforms4 that way, you can use the font in your program without worrying about the details except in the one place the font is actually defined. $ere's an example(
font+c$eate+AppHig"lig"tFont+,family+Helvetica+ ,si&e+-.+,weig"t+(old g$id+/ttk00la(el+1l+,text+"Attention2"+,font+ AppHig"lig"tFont3 AppHig"lig"tFont+4+TkFont1new+0family+45+ 6Helvetica67+0si&e+45+-.7+0weig"t+45+6(old6 Tk00Tile00'a(el1new8$oot9+:text+6Attention26;+ font+AppHig"lig"tFont<1g$id Tkx00font=c$eate8"AppHig"lig"tFont"7+,family+45+ "Helvetica"7+,si&e+45+-.7+,weig"t+45+"(old"9; >l+4+>parent,5new=ttk==la(el8,text+45+ "Attention2"7+,font+45+"AppHig"lig"tFont"9; >l,5g=g$id; f$om+tkinte$+impo$t+font appHig"lig"tFont+4+ font1Font8family46Helvetica67+si&e4-.7+ weig"t46(old69 ttk1'a(el8$oot7+text46Attention267+ font4appHig"lig"tFont91g$id89
The "family" specifies the font name4 the names Cou$ie$, Times, and Helvetica are guaranteed to be supported /and mapped to an appropriate monospaced, serif, or sans!serif font1, but other fonts installed on the system can be used /again, be careful to ensure the font exists, or the system will supply a different font, which may not necessarily be a good match1. 3ou can get the names of all available fonts with(
Font Descriptions
Another way to specify fonts is via a list of attributes, starting with the name of the font, and then optionally including a si5e, and optionally one or more style options. Some examples of this are "Helvetica", 0$elvetica .60, "Helvetica + -. + (old", and "Helvetica + -. + (old+ italic". These font descriptions are then used as the value of the "font"configuration option, rather than a predefined or named font. n general, switching from font descriptions to named fonts is ad!isable, again to isolate font differences in one location in the program.
Colors
As with fonts, there are various ways to specify colors. ull details can be found in the colors command reference. +n general, the system will provide the right colors for most things. %ike with fonts, both "ac and Windows specify a large number of system!specific color names /see the reference1, whose actual color may depend upon system settings /e.g. text highlight colors, default backgrounds1. 3ou can also specify colors via #)7, like in $T"%, e.g. "?@FF" or "?FFA-BA". inally, Tk recogni5es the set of color names
defined by -..4 normally these are not used, except for very common ones such as "$ed", "(lack", etc. or themed Tk widgets, colors are often used in defining styles that are applied to widgets, rather than applying the color to a widget directly. +t probably goes without saying that restraint in the use of colors is normally warranted.
Images
We've seen the basics of how to use images already, displaying them in labels or buttons for example. We create an image ob8ect, usually from a file on disk.
image+c$eate+p"oto+imgo(C+,file+"myimage1gif" 1la(el+configu$e+,image+imgo(C image+4+Tk!"otoImage1new80file+45+ "myimage1gif"9 la(el/6image63+4+image Tkx00image=c$eate=p"oto8"imgo(C"7+,file+45+ "myimage1gif"9; >l,5configu$e8,image+45+"imgo(C"9; imgo(C+4+!"otoImage8file46myimage1gif69 la(el/6image63+4+imgo(C
,ut of the box, Tk includes support for )+ and ''"9':" images. $owever, there is a Tk extension library called 0+mg0 which adds support for many others( 7"', -7", -'", ':), ;'<), T+ , etc. Though not included directly in the Tk core, +mg is usually included with other packaged distributions /e.g. ActiveTcl1.
Tk's images are actually =uite powerful and sophisticated, and provide a wide variety of ways to inspect and modify images. 3ou can find out more from the image command reference and the photo command reference.