Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python
All widgets provide reasonable default values.
Fonts are usually specified using the font widget option.
Tkinter supports a number of different font descriptor types:
- Font descriptors
- User-defined font names
- System fonts
- X font descriptors
Tkinter supports platform independent font descriptors.
A font can be specified as tuple containing a family name, a height in points, and optionally a string with one or more styles.
("Times",12 , "bold") ("Helvetica", 12, "bold italic") ("Verdana", 10)
To get the default size and style, you can give the font name as a single string.
If the family name doesn’t include spaces, you can also add size and styles to the string itself:
"Times 12 bold" "Helvetica 12 bold italic" "Symbol 10"
Here are some families available on most Windows platforms:
Arial (corresponds to Helvetica), Courier New (Courier), Comic Sans MS, Fixedsys, MS Sans Serif, MS Serif, Symbol, System, Times New Roman (Times), and Verdana.
The available styles are normal, bold, roman, italic, underline, and overstrike.
Tk 8.0 automatically maps Courier, Helvetica, and Times to their corresponding native family names on all platforms.
The tkFont module provides a Font class which allows you to create font instances.
Such instance can be used everywhere for Tkinter accepts a font specifier.
Font instance is also used to get font metrics, including the size occupied by a given string written in that font.
tkFont.Font(family="Times", size=12, weight=tkFont.BOLD) tkFont.Font(family="Helvetica", size=12, weight=tkFont.BOLD, slant=tkFont.ITALIC) tkFont.Font(family="Symbol", size=10)
If you modify a named font using the config method, the changes are automatically propagated to all widgets using the font.
The Font constructor supports the following style options (note that the constants are defined in the tkFont module):
|size||Font size in points. To give the size in pixels, use a negative value.|
|weight||Font thickness. Use one of NORMAL or BOLD. Default is NORMAL.|
|slant||Font slant. Use one of NORMAL or ITALIC. Default is NORMAL.|
|underline||Font underlining. If 1 (true), the font is underlined. Default is 0 (false).|
|overstrike||Font strikeout. If 1 (true), a line is drawn over text written with this font. Default is 0 (false).|
- Tk also supports system specific font names.
- Under Windows, these include ansi, ansifixed, device, oemfixed, system, and systemfixed.
- On the Macintosh, the system font names are application and system.
- While text labels and buttons usually contain a single line of text, Tkinter also supports multiple lines. To split the text across lines, simply insert newline characters (n) where necessary.
- By default, the lines are centered. You can change this by setting the justify option to LEFT or RIGHT. The default value is CENTER.
- You can also use the wraplength option to set a maximum width, and let the widget wrap the text over multiple lines all by itself.
- Tkinter attempts to wrap on whitespace, but if the widget is too narrow, it may break individual words across lines.
System fonts are full font names, not family names, and they cannot be combined with size or style attributes.
""" Author : ITVoyagers (itvoyagers.in) Date :17th March 2020 Description : Program to show use of font in python """ from tkinter import * parent = Tk() Label(parent, text="Pink Text in Times Font",fg = "pink",bg = "dark green",font = "Times").pack() Label(parent, text="Purple Text in Helvetica Font",fg = "purple",bg = "pink",font = "Helvetica 16 bold italic").pack() Label(parent,text="Blue Text in Verdana bold",fg = "blue",bg = "yellow",font = "Verdana 10 bold").pack()
All Tkinter widgets have a border (though it’s not visible by default for some widgets).
The border consists of an optional 3D relief, and a focus highlight region.
The relief settings control how to draw the widget border:
- borderwidth (or bd): This is the width of the border, in pixels. Most widgets have a default borderwidth of one or two pixels. There’s hardly any reason to make the border wider than that. relief
- This option controls how to draw the 3D border. It can be set to one of SUNKEN, RAISED, GROOVE, RIDGE, FLAT and SOLID.
""" Author : ITVoyagers (itvoyagers.in) Date :17th March 2020 Description : Program to show use of relief styles. """ from tkinter import * itv =Tk() Button(itv,text="Relief Style:Flat",relief=FLAT).pack() Button(itv,text="Relief Style:RAISED",relief=RAISED).pack() Button(itv,text="Relief Style:SUNKEN",relief=SUNKEN).pack() Button(itv,text="Relief Style:GROOVE",relief=GROOVE).pack() Button(itv,text="Relief Style:RIDGE",relief=RIDGE).pack() Button(itv,text="Relief Style:SOLID",relief=SOLID).pack()
The highlight settings control how to indicate that the widget (or one of its children) has keyboard focus.
In most cases, the highlight region is a border outside the relief.
The following options control how this extra border is drawn:
This option is used to draw the highlight region when the widget has keyboard focus. It’s usually black, or some other distinct contrast color.
This option is used to draw the highlight region when the widget doesn’t have focus. It’s usually same as the widget background.
This option is the width of the highlight region, in pixels. It is usually one or two pixels for widgets that can take keyboard focus.
X Font Descriptors
X Font Descriptors are strings having the following format (the asterisks represent fields that are usually not relevant. For details, see the Tk documentation, or an X manual):
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 (another word for italic).
Size is the height of the font in decipoints (that is, points multiplied by 10). There are 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.
You can also check following posts to know more about tkinter and GUI
- What is tkinter?
- Layout / Geometry Manager in Python
- GUI programming in Python and Python GUI Library
- Events and Bindings in Python along with Widget configuration and styling
- Widgets in tkinter module – Python
- Label, Text, Entry & Message Widget in Python
- Button, Checkbutton & Radiobutton Widget in Python
- Best post on Menu and Menubutton Widget of tkinter
- Best post- Listbox and Scrollbar(Slider) Widget-Python
- Best post on Frame Widget of tkinter in Python
- Best post on Spinbox and Scale Widget in Python
- Best post- LabelFrame, Toplevel, PanedWindow widgets
- Best post: message box widget and its methods
- Best post:Dimensions, Anchors, Bitmaps & Cursors in Python