Version 0 of named fonts

Updated 2004-12-07 16:47:54

Named fonts are a great way to create and modify fonts on an application-wide basis. Just create a named font with [font create], then use that font name to create widgets or text elements. Then later you can use [font configure] to modify the named font, and all the text elements will be automagically updated. Here is a little code example, showing named fonts used both in a widget and on a canvas.

    package require Tk

    font create titleFont -family Helvetica -size 12
    font create subtitleFont -family Helvetica -size 10


    canvas .c -borderwidth 3 -relief sunken
    .c create text 20 30 -anchor w -text "This is the Title" -font titleFont
    .c create text 20 50 -anchor w -text "This is the subtitle" -font subtitleFont

    checkbutton .cb -text "Title Bold" \
              -variable titleBold -offvalue normal -onvalue bold \
              -command {font configure titleFont -weight $titleBold}

    label .l1 -text "Family"
    entry .e1 -textvariable subFamily
    label .l2 -text "Size"
    entry .e2 -textvariable subSize
    button .b3 -text "Apply" -command applySub -font titleFont

    grid  .c  -  -sticky news
    grid  x  .cb -sticky w
    grid .l1 .e1 -sticky e
    grid .l2 .e2 -sticky e
    grid  x  .b3
    set subFamily [font configure subtitleFont -family]
    set subSize   [font configure subtitleFont -size]

    proc applySub {} {
        global subFamily subSize
        font configure subtitleFont -family $subFamily -size $subSize
    }

See also font, A little font chooser and Changing Fonts in an Application