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.
Standard Fonts
The predefined fonts are:
TkDefaultFont | The default for all GUI items not otherwise specified. |
TkTextFont | Used for entry widgets, listboxes, etc. |
TkFixedFont | A standard fixed-width font. |
TkMenuFont | The font used for menu items. |
TkHeadingFont | The font typically used for column headings in lists and tables. |
TkCaptionFont | A font for window and dialog caption bars. |
TkSmallCaptionFont | A smaller caption font for subwindows or tool dialogs |
TkIconFont | A font for icon captions. |
TkTooltipFont | A font for tooltips. |
Platform-Specific Fonts
A number of additional predefined fonts are available, but the precise set depends on the platform.
Named Fonts
Here's an example:
from tkinter import font
appHighlightFont = font.Font(family='Helvetica', size=12, weight='bold')
ttk.Label(root, text='Attention!', font=appHighlightFont).grid()
You can get the names of all available fonts with:
font.families()
The
"size" option specifies the size of the font, in points. The
"weight" option can be either
bold or
normal. You can specify a
"slant" of
roman (normal) or
italic.Finally, the boolean options
"underline" and
"overstrike" are available.
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 size, and optionally one or more style options. Some examples of this are "Helvetica", "Helvetica 12","Helvetica 12 bold", and "Helvetica 12 bold italic". These font descriptions are then used as the value of the "font" configuration option, rather than a predefined or named font.
Colors
You can also specify colors via RGB, like in HTML, e.g. "#3FF" or "#FF016A".Finally, Tk recognizes the set of color names defined by X11; normally these are not used,except for very common ones such as "red", "black", etc.
Images
We create an image object, usually from a file on disk.
imgobj = PhotoImage(file='myimage.gif')
label['image'] = imgobj
Out of the box, Tk includes support for GIF and PPM/PNM images. However, there is a Tk extension library called "Img" which adds support for many others: BMP, XBM, XPM, PNG, JPEG,TIFF, etc. Though not included directly in the Tk core, Img is usually included with other packaged distributions (e.g. ActiveTcl).