我正在运行 Python 3.6 并且想知道是否有办法获取 Tkinter 使用的默认字体,更具体地说是Canvas
对象在调用canvas.create_text
时使用的默认字体。
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.
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.
I believe this will solve your problem.
from tkinter import *
janela = Tk()
label = Label(janela)
print(label["font"])
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.