Python 列出 `tkinter` 中可用的字体系列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39614027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:30:53  来源:igfitidea点击:

List available font families in `tkinter`

pythontkinter

提问by Peque

In many tkinterexamples available out there, you may see things like:

在许多tkinter可用的示例中,您可能会看到以下内容:

canvas.create_text(x, y, font=('Helvetica', 12), text='foo')

However, this may not work when run in your computer (the result would completely ignore the font parameter). Aparently, the fontparameter is ignored if there is any incorrect value in it.

但是,这在您的计算机中运行时可能不起作用(结果将完全忽略字体参数)。显然,font如果参数中有任何不正确的值,则该参数将被忽略。

In order to check if the font family is valid, how can I list all available in my system?

为了检查字体系列是否有效,我如何列出系统中所有可用的字体?

回答by Peque

from tkinter import Tk, font
root = Tk()
font.families()

回答by jimmiesrustled

This question has been answered fully, but it's useful for me to be able to see what every font looks like so that I'm sure of which one I would like to use. In the interest of saving someone else from reinventing the wheel, I'll post my code here. As above, each font family is shown in a scrolling window. The difference is that each font in this code is printed in the font itself.

这个问题已经得到了完整的回答,但对我来说能够看到每种字体的外观很有用,这样我就可以确定我想使用哪种字体。为了避免其他人重新发明轮子,我将在此处发布我的代码。如上所述,每个字体系列都显示在一个滚动窗口中。不同之处在于此代码中的每个字体都打印在字体本身中。

from tkinter import *
from tkinter import font

root = Tk()
root.title('Font Families')
fonts=list(font.families())
fonts.sort()

def populate(frame):
    '''Put in the fonts'''
    listnumber = 1
    for item in fonts:
        label = "listlabel" + str(listnumber)
        label = Label(frame,text=item,font=(item, 16)).pack()
        listnumber += 1

def onFrameConfigure(canvas):
    '''Reset the scroll region to encompass the inner frame'''
    canvas.configure(scrollregion=canvas.bbox("all"))

canvas = Canvas(root, borderwidth=0, background="#ffffff")
frame = Frame(canvas, background="#ffffff")
vsb = Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw")

frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))

populate(frame)

root.mainloop()

I hope this helps somebody.

我希望这对某人有帮助。

回答by Gary02127

There are two sources of available fonts that I can think of:
1. Font Families, and
2. Named Fonts.

我能想到的可用字体有两种来源:
1. 字体系列,和
2. 命名字体。

import sys
if sys.version_info.major is 3:
    import tkinter as Tk, tkinter.font as tkFont
else:
    import Tkinter as Tk, tkFont
root = Tk.Tk()
print(tkFont.families())
print(tkFont.names())

All the available, predefined font families (like 'Courier', 'Helvetica', and numerous others) should be found in the tkFont.families()call.

所有可用的预定义字体系列(如“Courier”、“Helvetica”等)都应在tkFont.families()调用中找到。

Named fonts are a bit different. Whenever you create a new font instance with tkFont.Font(...), you're creating a named font, and you get back the name of the new font instance. That name will show up in the tkFont.names()list. That list comes pre-filled with all the named fonts used as defaults for different widgets. If you see a font identifier you don't recognize, like 'font1234567' or 'TkFixedFont', it's probably the name of a named font, which you can easily access and use via font = tkFont.Font(name=<fontname>).

命名字体有点不同。每当您使用 来创建新字体实例时tkFont.Font(...),您就是在创建一个命名字体,并且您会返回新字体实例的名称。该名称将显示在tkFont.names()列表中。该列表预先填充了用作不同小部件默认值的所有命名字体。如果您看到不认识的字体标识符,例如“font1234567”或“TkFixedFont”,则它可能是命名字体的名称,您可以通过font = tkFont.Font(name=<fontname>).

回答by Ravikirana B

from tkinter import *
from tkinter import font
root=Tk()
fonts=font.families()
for i in fonts:
    print(i)

回答by Marty Wilk

This is a cleaner view of all the fonts in a scroll thru window.

这是滚动窗口中所有字体的清晰视图。

from tkinter import *
from tkinter import font


root = Tk()
root.title('Font Families')
fonts=list(font.families())
fonts.sort()

display = Listbox(root)
display.pack(fill=BOTH, expand=YES, side=LEFT)

scroll = Scrollbar(root)
scroll.pack(side=RIGHT, fill=Y, expand=NO)

scroll.configure(command=display.yview)
display.configure(yscrollcommand=scroll.set)

for item in fonts:
    display.insert(END, item)

root.mainloop()

回答by edwin

The snippet of code below will show a list of all the available fonts that Tkinter has to offer, using tkFontand the font.families().

下面的代码片段将显示 Tkinter 必须提供的所有可用字体的列表,使用tkFontfont.families().

from Tkinter import *
import tkFont

root = Tk()

fonts=list(tkFont.families())
fonts.sort()

display = Listbox(root)
display.pack(fill=BOTH, expand=YES, side=LEFT)

scroll = Scrollbar(root)
scroll.pack(side=RIGHT, fill=Y, expand=NO)

scroll.configure(command=display.yview)
display.configure(yscrollcommand=scroll.set)

for item in fonts:
    display.insert(END, item)

root.mainloop()