Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python

Fonts Names, Font Descriptors, System Fonts, Text formatting, Borders, Relief Styles in Python

Fonts

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
  • 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.

    Example

    ("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.

    Font Names

    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):

    Font StyleDescription
    familyFont family
    sizeFont size in points. To give the size in pixels, use a negative value.
    weightFont thickness. Use one of NORMAL or BOLD. Default is NORMAL.
    slantFont slant. Use one of NORMAL or ITALIC. Default is NORMAL.
    underlineFont underlining. If 1 (true), the font is underlined. Default is 0 (false).
    overstrikeFont strikeout. If 1 (true), a line is drawn over text written with this font. Default is 0 (false).


    System Fonts

    • 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.
    • Note :
      System fonts are full font names, not family names, and they cannot be combined with size or style attributes.

      Text Formatting

      • 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.

      • Example:

        """
        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()
        

        OUTPUT

        font

        Borders

        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.

        Relief

        The relief settings control how to draw the widget border:

Leave a Comment