5

I'm running Python 3.6 and was wondering if there is a way to get the default font that Tkinter uses, more specifically the default font that the Canvas object uses when canvas.create_text is called.

Ryan
  • 727
  • 2
  • 14
  • 31

4 Answers4

1

idlelib/help.py has this line:

    normalfont = self.findfont(['TkDefaultFont', 'arial', 'helvetica'])

where findfont is defined thusly:

def findfont(self, names):
    "Return name of first font family derived from names."
    for name in names:
        if name.lower() in (x.lower() for x in tkfont.names(root=self)):
            font = tkfont.Font(name=name, exists=True, root=self)
            return font.actual()['family']
        elif name.lower() in (x.lower()
                              for x in tkfont.families(root=self)):
            return name

(I did not write this.)

https://www.tcl.tk/man/tcl8.6/TkCmd/font.htm is the ultimate doc on font functions.

Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
0

From the documentaion here:

Tk 8.0 automatically maps Courier, Helvetica, and Times to their corresponding native family names on all platforms.

I cannot find documentation that says what the default font for canvas.create_text would be but it should be one of the 3 listed in the above quote.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
0

I believe this will solve your problem.

from tkinter import *


janela = Tk()
label = Label(janela)
print(label["font"])
-1

Yes. The default font used to create a text object on a canvas is "TkDefaultFont"

from tkinter import *
r = Tk()
c = Canvas(r)
c.pack()
id = c.create_text(10, 10, text='c')
def_font = c.itemconfig(id, 'font')[-2] # [-2] is default, [-1] is current
print(def_font, c.itemconfig(id)) # to see all the config info

If you wanted to modify that default font in place, you would use nametofont() to get access to the underlying font object and then manipulate it:

from tkinter import font
def_font_obj = font.nametofont(def_font)
def_font_obj.config(...)

If you don't want to customize a default font, you can create a new named font based on the current font and then modify that with

current_font = c.itemconfig(id, 'font')[-1] # or just c.itemcget(id, 'font')
new_named_font = font.Font(font=current_font).config(...)

then pass in the new_named_font as a font option to any widget config.

GaryMBloom
  • 5,350
  • 1
  • 24
  • 32