【问题标题】:I get an error while creating a instance of the Font class in Tkinter在 Tkinter 中创建 Font 类的实例时出现错误
【发布时间】:2023-01-12 01:44:00
【问题描述】:

以下行:

my_font = tk.font.Font(size=20)

给我以下错误:

模块 tkinter 没有属性 font

如何解决这个问题。

【问题讨论】:

  • 您是否明确导入了tkinter.font

标签: python tkinter


【解决方案1】:

font 与主 tkinter 分开导入

这应该适合你

import tkinter as tk
from tkinter.font import Font

然后你可以这样做

root = tk.Tk()
my_font = Font(root, size=20)

【讨论】:

  • RuntimeError:使用字体为时过早:没有默认的根窗口
  • @toyotaSupra 是的,不用说你不能自己运行这段代码。我对其进行了编辑以包含 rootTk 实例 - 我认为这样更清楚
【解决方案2】:

应该是这样的。

import tkinter.font as tkFont
 
myFont = tkFont.Font(root, size=20)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-20
    • 2013-08-27
    • 2017-12-12
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多