You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中import *与重复导入特定元素的技术疑问

Why do we need from tkinter import font after from tkinter import *?

Great question—this is a super common point of confusion when starting out with Python's tkinter library, so let's break it down clearly:

  • First off, from tkinter import * doesn't actually import everything in the tkinter package. It only pulls in the names listed in the tkinter module's __all__ variable. This list includes the most frequently used classes and functions from the main tkinter module, like Tk, Button, Label, and Frame—but it does not include submodules such as tkinter.font.

  • tkinter.font is a standalone submodule within the tkinter package, with its own dedicated tools (like the Font class for customizing text styles, sizes, and weights). Since it's not part of the main module's __all__, the wildcard * import won't make this submodule available in your code's namespace. If you tried to use font.Font(...) without explicitly importing the font submodule, Python would throw a NameError because it has no idea what font refers to.

  • To make this concrete: If you only run from tkinter import *, you can create a basic button with Button(root, text="Click Me"), but you won't be able to tweak its font until you add from tkinter import font (or use import tkinter.font and reference it as tkinter.font.Font(...)).

As a quick side note, many Python developers avoid from module import * entirely because it can lead to naming conflicts (if two modules share the same name for a class/function), but now you know exactly why that extra import line is necessary when you encounter this pattern!

内容的提问来源于stack exchange,提问作者Claudiu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:37:02