为什么我得到TypeError:“module”对象不能使用tkinter字体调用

2024-09-28 22:25:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Tkinter GUI创建一个Python桌面应用程序。
代码如下:

from tkinter import *
from tkinter import ttk
import random
import time
import datetime
import tkinter.messagebox
import tkinter.font as Font


# Define Font
titleFont = Font(family="Arial", size="48")

我正在努力思考这个错误是如何造成的:

当我运行titleFont = Font(family="Arial", size="48")时,我得到:

TypeError: 'module' object is not callable


Tags: 代码fromimport应用程序sizetkinterguirandom
1条回答
网友
1楼 · 发布于 2024-09-28 22:25:53

假设您的代码是导入tkinter.font模块(作为Font)并尝试调用它。但是Python模块是不可调用的。我想您正在尝试从tkinter.font模块导入Font。你知道吗

注意,您必须首先创建一个根窗口。你知道吗

以下是修订后的代码:

from tkinter import *
from tkinter.font import Font


# Define Font
root = Tk()  # create the root window
root.title("Hello, World!")  # set the title of the root window
titleFont = Font(family="Arial", size="48")  # create the Font object (don't forget to specify its master!)
Label(root, text="Hello, World!", font=titleFont).pack()  # create a label to preview the font
root.mainloop()  # start the root window's mainloop

相关问题 更多 >