Fonts in R TclTk

Fonts in R TclTk

James Wettenhall & Philippe Grosjean bio photo By James Wettenhall & Philippe Grosjean

The following example illustrates how to specify the font to be used in Tk windows/widgets. If you explicitly add the option font = myFont to every widget you create (where possible), then you only have to change myFont in one place, if for example you wish to use a bigger font for a presentation with a projector. You may wish to define a few different fonts - one fixed width font, one font for headings, etc.


win1 <- tktoplevel()

fontHeading <- tkfont.create(family = "Arial", size = 24,
weight = "bold", slant = "italic")
fontTextLabel <- tkfont.create(family = "Times New Roman", size = 12)
fontFixedWidth <- tkfont.create(family = "Courier New", size = 12)

tkgrid(tk2label(win1, text = "A Nice Big Font for the Heading",
font = fontHeading), padx = 10, pady = 15)
tkgrid(tk2label(win1, text = "A normal text label.",
font = fontTextLabel), padx = 10, pady = 5)
tkgrid(tk2label(win1, text = "A fixed width font.",
font = fontFixedWidth, background = "white"), padx = 10, pady = c(5, 15))


Font selector

The tcltk2 package proposes a font-selector dialog box:

fontHeading2 <- tk2chooseFont(font = fontHeading)
tkgrid(tk2label(win1, text = "A heading with the new font",
font = fontHeading2), padx = 10, pady = 15)

Select a different font, then…

font selector

… click OK.

fonts updated