Python中import *与重复导入特定元素的技术疑问
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, likeTk,Button,Label, andFrame—but it does not include submodules such astkinter.font.tkinter.fontis a standalone submodule within the tkinter package, with its own dedicated tools (like theFontclass 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 usefont.Font(...)without explicitly importing thefontsubmodule, Python would throw aNameErrorbecause it has no idea whatfontrefers to.To make this concrete: If you only run
from tkinter import *, you can create a basic button withButton(root, text="Click Me"), but you won't be able to tweak its font until you addfrom tkinter import font(or useimport tkinter.fontand reference it astkinter.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

