Tk fonts and colours
last modified November 24, 2026 Tcl 8.6
Every piece of text in Tk is drawn with a font, and every font is drawn in a
colour. A font description holds a family, a size and a few style flags; a
colour is either an X11 name such as navy or a value written
#rrggbb. Both are ordinary strings, so they can be stored in
variables or read from a configuration file.
This article covers the font command, colour names, RGB values
and the colours that ttk widgets take from their theme.
Font families
A font family is the name of a typeface, for example DejaVu Sans
or Times New Roman. The font families command
returns the families found on the current machine.
package require Tk
set families [lsort [font families]]
puts "Tk knows [llength $families] font families."
puts "First four: [join [lrange $families 0 3] ", "]"
puts "DejaVu Sans present: [expr {[lsearch -exact $families "DejaVu Sans"] >= 0}]"
set mono [list "DejaVu Sans Mono" 10]
puts "DejaVu Sans Mono is fixed: [font metrics $mono -fixed]"
destroy .
The sort is only for display. A family name with spaces must stay one word in
a font description, which is why the script builds it with list.
The braced form {DejaVu Sans 10} is three words and would take
Sans as the size.
$ wish families.tcl Tk knows 666 font families. First four: Abyssinica SIL, Accanthis ADF Std, Accanthis ADF Std No2, Accanthis ADF Std No3 DejaVu Sans present: 1 DejaVu Sans Mono is fixed: 1
Named fonts with font create
A font description passed straight to a widget cannot be changed afterwards.
A named font solves that: font create registers a font under a
name, and every widget that uses the name follows later changes.
package require Tk
font create heading_font -family "DejaVu Sans" -size 18 -weight bold
font create body_font -family "DejaVu Serif" -size 11
font create code_font -family "DejaVu Sans Mono" -size 10
puts "heading_font: [font configure heading_font]"
puts "body family: [font actual body_font -family]"
puts "code fixed: [font metrics code_font -fixed]"
puts "registered: [expr {[lsearch -exact [font names] heading_font] >= 0}]"
destroy .
font names lists every named font, including the ones Tk uses
for its widgets, such as TkDefaultFont. Named fonts belong to the
interpreter rather than to a window, so two toplevels can share one.
$ wish named_fonts.tcl
heading_font: -family {DejaVu Sans} -size 18 -weight bold -slant roman -underline 0 -overstrike 0
body family: DejaVu Serif
code fixed: 1
registered: 1
Font configure and actual
font configure changes a named font and returns its options when
called without new values. The options are -family,
-size, -weight, -slant,
-underline and -overstrike.
package require Tk font create label_font -family "DejaVu Sans" -size 12 -weight normal puts "start: weight [font configure label_font -weight], size [font configure label_font -size]" font configure label_font -weight bold -slant italic -underline 1 puts "now: weight [font configure label_font -weight], slant [font configure label_font -slant]" puts "now: underline [font configure label_font -underline]" puts "actual family: [font actual label_font -family]" puts "actual size: [font actual label_font -size]" puts "actual slant: [font actual label_font -slant]" puts "all options: [font actual label_font]" destroy .
font configure reports the request, while
font actual reports what the font engine resolved it to. The
difference matters when a family is missing, because Tk then substitutes a
similar one and only font actual reveals it.
$ wish configure.tcl
start: weight normal, size 12
now: weight bold, slant italic
now: underline 1
actual family: DejaVu Sans
actual size: 12
actual slant: italic
all options: -family {DejaVu Sans} -size 12 -weight bold -slant italic -underline 1 -overstrike 0
Font metrics
Metrics describe the space a font needs. The ascent is the distance above the
baseline, the descent the distance below it, and the linespace the height of
one line. The -fixed flag tells whether every character has the
same width.
package require Tk
set f [list "DejaVu Sans Mono" 12]
puts "ascent: [font metrics $f -ascent]"
puts "descent: [font metrics $f -descent]"
puts "linespace: [font metrics $f -linespace]"
puts "fixed: [font metrics $f -fixed]"
puts "W width: [font measure $f W]"
puts "iiii width: [font measure $f iiii]"
puts "10 rows: [expr {10 * [font metrics $f -linespace]}] pixels"
destroy .
In a monospaced font W and iiii have the same width,
so the two measurements differ only by the character count. The ascent and
descent add up to the linespace, and a canvas or text widget uses these
numbers to size a row of text.
$ wish metrics.tcl ascent: 16 descent: 4 linespace: 20 fixed: 1 W width: 10 iiii width: 40 10 rows: 200 pixels
Using fonts in widgets
Every widget that displays text has a -font option, which
accepts a named font or a plain description. Several widgets can therefore
share one font object, the usual way to keep a window consistent.
package require Tk
font create title_font -family "DejaVu Serif" -size 20 -weight bold
font create body_font -family "DejaVu Sans" -size 11
wm title . "Fonts in widgets"
label .title -text "Fonts and colours" -font title_font
label .body -text "This label uses a named font." -font body_font
button .close -text "Close" -font body_font -command {destroy .}
pack .title -padx 12 -pady 8
pack .body -padx 12
pack .close -pady 8
set used [.title cget -font]
puts "title widget uses: $used"
puts "its size: [font configure $used -size]"
puts "rendered width: [font measure $used "Fonts and colours"]"
after 500 {destroy .}
cget -font returns the name of the font object, not a copy of its
options, which shows that the widget is still connected to the named font. A
later font configure title_font -size 26 would redraw the label
without any other change.
$ wish widgets.tcl title widget uses: title_font its size: 20 rendered width: 282
Colours by name
Tk ships with the X11 colour names: red, navy,
gold, DarkRed, gray70 and several
hundred more, all case-insensitive. winfo rgb converts a name
into three intensities between 0 and 65535.
package require Tk
foreach name {red DarkRed gold navy white} {
puts [format "%-8s %s" $name [winfo rgb . $name]]
}
puts "RED gives the same: [winfo rgb . RED]"
puts "unknown name: [catch {winfo rgb . nosuchcolour} err] $err"
destroy .
The first argument is a window, because colour names belong to a display;
. is the main window and works for the whole application. An
unknown name is an error rather than an empty result, and the script catches
it to print the message.
$ wish colours.tcl red 65535 0 0 DarkRed 35723 0 0 gold 65535 55255 0 navy 0 0 32896 white 65535 65535 65535 RED gives the same: 65535 0 0 unknown name: 1 unknown color name "nosuchcolour"
RGB values with #rrggbb
A hexadecimal colour is # followed by three pairs of digits, one
pair each for red, green and blue. Each pair runs from 00 to
ff, so #3a6ea5 means 58 red, 110 green and 165
blue. Tk accepts the value wherever it accepts a colour name.
package require Tk
foreach colour {#f8f8f8 #c0c0c0 #808080 #3a6ea5} {
lassign [winfo rgb . $colour] r g b
puts "$colour -> [format "%3d %3d %3d" [expr {$r / 257}] [expr {$g / 257}] [expr {$b / 257}]]"
}
label .swatch -text "accent" -background #3a6ea5 -foreground #ffffff
pack .swatch -padx 20 -pady 20
puts "widget background: [.swatch cget -background]"
after 500 {destroy .}
The script converts the 16-bit components back to the 8-bit numbers of the hexadecimal notation; dividing by 257 is exact because 65535 is 255 times 257. The same syntax is used by the Tk image commands, where pixel values are written and read.
$ wish rgb.tcl #f8f8f8 -> 248 248 248 #c0c0c0 -> 192 192 192 #808080 -> 128 128 128 #3a6ea5 -> 58 110 165 widget background: #3a6ea5
Theme colours
ttk widgets do not paint their own colours; they ask the current theme, which
defines a background, a foreground and a border colour for every widget
class. ttk::style lookup reads those values and
ttk::style configure overrides them for a named style.
package require Tk puts "themes: [ttk::style theme names]" puts "current: [ttk::style theme use]" ttk::style theme use clam puts "button bg: [ttk::style lookup TButton -background]" puts "label fg: [ttk::style lookup TLabel -foreground]" puts "frame bg: [ttk::style lookup TFrame -background]" ttk::style configure Danger.TButton -background #b03030 -foreground white puts "custom bg: [ttk::style lookup Danger.TButton -background]" destroy .
The available themes depend on the platform and the Tk build; this system
offers clam, alt, default and
classic. A custom style is named by prefixing the class, so
Danger.TButton is applied with -style Danger.TButton.
$ wish theme.tcl themes: clam alt default classic current: default button bg: #dcdad5 label fg: black frame bg: #dcdad5 custom bg: #b03030
A small palette example
The last script stores a palette as a dictionary that maps a role name to a hexadecimal colour, then builds one frame and one label per entry. The same loop prints the components, so the palette can be checked against a design document.
package require Tk
wm title . "Colour palette"
set palette {
ink #1f2933
paper #f5f7fa
accent #3a6ea5
warning #c07000
success #2f7d4f
}
set row 0
foreach {name colour} $palette {
frame .swatch_$name -background $colour -width 90 -height 26
label .name_$name -text "$name $colour" -anchor w
grid .swatch_$name -row $row -column 0 -padx 10 -pady 4
grid .name_$name -row $row -column 1 -sticky w -padx 10
incr row
}
foreach {name colour} $palette {
lassign [winfo rgb . $colour] r g b
puts [format "%-8s %-8s -> %3d %3d %3d" $name $colour \
[expr {$r / 257}] [expr {$g / 257}] [expr {$b / 257}]]
}
after 700 {destroy .}
The dictionary is read twice, once for the widgets and once for the printed values, which is the normal pattern for configuration data in Tk. Widget names are built from the role name, so they stay unique, and only the swatch frames carry the palette colour.
$ wish palette.tcl ink #1f2933 -> 31 41 51 paper #f5f7fa -> 245 247 250 accent #3a6ea5 -> 58 110 165 warning #c07000 -> 192 112 0 success #2f7d4f -> 47 125 79
Source
The font command, the colour names and the ttk style system are
documented in the Tk manual.
This article covered fonts, metrics, colour names, RGB values and theme colours. Continue with Tk images, which uses the same hexadecimal colours for pixel data, or read the Tcl after page for the timer that redraws a widget.
Author
List all Tcl Tutorials.