Python3 Tkinter 字体不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32660387/
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 12:00:17  来源:igfitidea点击:

Python3 Tkinter fonts not working

pythonpython-3.xfontstkinterpython-import

提问by percidae

I am using python 3.3 with tkinter, and the package python3-tk is installed. In most docs the old "import tkFont" is used, which is not working any more.

我正在将 python 3.3 与 tkinter 一起使用,并且安装了 python3-tk 包。在大多数文档中,使用旧的“import tkFont”,它不再起作用。

This is supposed to work:

这应该有效:

from tkinter import font
appHighlightFont = font.Font(family='Helvetica', size=12, weight='bold')
font.families()

However, I get this exception on the second line:

但是,我在第二行收到此异常:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.3/tkinter/font.py", line 92, in __init__
    root.tk.call("font", "create", self.name, *font)
AttributeError: 'NoneType' object has no attribute 'tk'

I checked http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/fonts.htmland http://www.tkdocs.com/tutorial/fonts.htmlwhich were the most useful tkinter docs so far.

我检查了http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/fonts.htmlhttp://www.tkdocs.com/tutorial/fonts.html这是迄今为止最有用的 tkinter 文档.

Unfortunately I still can't figure out what I am doing wrong.

不幸的是,我仍然无法弄清楚我做错了什么。

采纳答案by Bryan Oakley

You should import fontnot fonts. Also, if the code you posted is actual code, you are neglecting to create a root window before working with fonts. You must create a root window first.

你应该导入fontfonts。此外,如果您发布的代码是实际代码,则您忽略了在使用字体之前创建根窗口。您必须先创建一个根窗口。

from tkinter import font
import tkinter as tk
...
root = tk.Tk()
...
appHighlightFont = font.Font(family='Helvetica', size=12, weight='bold')
font.families()

回答by daBoss

from tkinter import *
from time import sleep

window = Tk()
window.geometry("350x400")
window.title("tkinter!")

lbl = Label(window, text="this is a lable", size=12)
lbl.pack()

sleep(1)

lbl.configure(text="now it is a big lable", size=48)