Next / Previous / Contents

5.4. Type fonts

Depending on your platform, there may be up to three ways to specify type style.

  • As a tuple whose first element is the font family, followed by a size (in points if positive, in pixels if negative), optionally followed by a string containing one or more of the style modifiers bold, italic, underline, and overstrike.

    Examples: ('Helvetica', '16') for a 16-point Helvetica regular; ('Times', '24', 'bold italic') for a 24-point Times bold italic. For a 20-pixel Times bold font, use ('Times', -20, 'bold').

  • You can create a “font object” by importing the tkFont module and using its Font class constructor:

    import tkFont
    
    font = tkFont.Font(option, ...)
    

    where the options include:

    family The font family name as a string.
    size The font height as an integer in points. To get a font n pixels high, use -n.
    weight'bold' for boldface, 'normal' for regular weight.
    slant 'italic' for italic, 'roman' for unslanted.
    underline 1 for underlined text, 0 for normal.
    overstrike 1 for overstruck text, 0 for normal.

    For example, to get a 36-point bold Helvetica italic face:

        helv36 = tkFont.Font(family='Helvetica',
            size=36, weight='bold')
    
  • If you are running under the X Window System, you can use any of the X font names. For example, the font named '-*-lucidatypewriter-medium-r-*-*-*-140-*-*-*-*-*-*' is a good fixed-width font for onscreen use. Use the xfontsel program to help you select pleasing fonts.

To get a list of all the families of fonts available on your platform, call this function:

    tkFont.families()

The return value is a list of strings. Note: You must create your root window before calling this function.

These methods are defined on all Font objects:

.actual(option=None)

If you pass no arguments, you get back a dictionary of the font's actual attributes, which may differ from the ones you requested. To get back the value of an attribute, pass its name as an argument.

.cget(option)

Returns the value of the given option.

.configure(option, ...)

Use this method to change one or more options on a font. For example, if you have a Font object called titleFont, if you call titleFont.configure(family='times', size=18), that font will change to 18pt Times and any widgets that use that font will change too.

.copy()

Returns a copy of a Font object.

.measure(text)

Pass this method a string, and it will return the number of pixels of width that string will take in the font. Warning: some slanted characters may extend outside this area.

.metrics(option)

If you call this method with no arguments, it returns a dictionary of all the font metrics. You can retrieve the value of just one metric by passing its name as an argument. Metrics include:

ascent Number of pixels of height between the baseline and the top of the highest ascender.
descent Number of pixels of height between the baseline and the bottom of the lowest ascender.
fixed This value is 0 for a variable-width font and 1 for a monospaced font.
linespace Number of pixels of height total. This is the leading of type set solid in the given font.