0% found this document useful (0 votes)
68 views6 pages

Fonts, Colors, Images

This document describes how Tk handles fonts, colors, and images. It discusses the standard fonts available in Tk, how to specify custom named fonts, and how to specify fonts using font descriptions. It also covers how to specify colors using predefined names, RGB values, and hex codes. Finally, it provides an overview of how to work with images in Tk, including creating image objects from files and using the Img extension to support additional image formats.

Uploaded by

Upendra Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views6 pages

Fonts, Colors, Images

This document describes how Tk handles fonts, colors, and images. It discusses the standard fonts available in Tk, how to specify custom named fonts, and how to specify fonts using font descriptions. It also covers how to specify colors using predefined names, RGB values, and hex codes. Finally, it provides an overview of how to work with images in Tk, including creating image objects from files and using the Img extension to support additional image formats.

Uploaded by

Upendra Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

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(

TkDefaultFont TkTextFont TkFixedFont TkMenuFont TkHeadingFont TkCaptionFont

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

TkSmallCaptionFont A smaller caption font for subwindows or tool TkIconFont TkTooltipFont


Platform-Specific Fonts
A number of additional predefined fonts are available, but the precise set depends on the platform. ,bviously, if you're using these, and your application is portable across different platforms, you'll need to ensure that proper fonts are defined individually for each platform. ,n -.., any valid -.. font name /see e.g. the 0xlsfonts0 command1 may be used. #emember though that there is no guarantee that a particular font has been installed on a particular machine. ,n Windows, the following font names, which map to the fonts that can be set in the 02isplay0 &ontrol 'anel, are available( system, ansi, device, systemfixed, ansifixed, oem fixed. ,n "ac ,S -, the following fonts are available /see the Apple $+) for details1( systemSystemFont,systemSmallSystemFont, systemApplicati onFont, systemViewsFont, systemMenuItemFont,systemMenuItemCmd eyFont, system!us"#uttonFont, systemAle$tHeade$Font, systemMini SystemFont,systemDetail%mp"asi&edSystemFont, system%mp"asi&ed A font for icon captions. A font for tooltips.

SystemFont, systemSmall%mp"asi&edSystemFont,system'a(elFont, s ystemMenuTitleFont, systemMenuItemMa$kFont, system)indowTitleF ont,system*tility)indowTitleFont, systemTool(a$Font, systemDet ailSystemFont.

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+families TkFont1families Tkx00font=families font1families89


The "si&e" option specifies the si5e of the font, in points. The "weig"t" option can be either (old or no$mal. 3ou can specify a "slant" of $oman /normal1 or italic. inally, the boolean options "unde$line" and "ove$st$ike" are available. The current settings of these options can be examined or changed using the same mechanisms that you'd use for changing the configuration options of a widget /e.g. configu$e1.

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.

package+$eDui$e+Img image+c$eate+p"oto+myimg+,file+"myimage1png" $eDui$e+6tkextli(Etkimg6 myimg+4+Tk!"otoImage1new80file+45+ 6myimage1png69 Tkx00package=$eDui$e8"Img"9; Tkx00image=c$eate=p"oto8"myimg"7+,file+45+ "myimage1png"9;


'3T$,:T,2,

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.

You might also like