为何从模块重复导入?关于tkinter导入方式的技术疑问
from module import *? Great question! This is a super common point of confusion, especially with libraries like tkinter, so let's break it down in plain terms.
1. from module import * doesn't actually import everything
Many modules define a special list called __all__ that explicitly lists which names should be exported when you use import *. Any names not in this list won't get pulled into your working namespace.
In the case of tkinter, the __all__ list doesn't include the ttk submodule. So when you run from tkinter import *, you get all the standard tkinter widgets (like Button, Label) and core functions, but ttk stays hidden in the parent module—you can't access it directly unless you import it separately.
2. Submodules are separate, self-contained entities
ttk is a distinct submodule within tkinter that provides modern, themed widgets. Even if a module doesn't use __all__, import * won't recursively import submodules. Think of modules like folders on your computer: opening the main folder (tkinter) doesn't automatically open all its subfolders (ttk, messagebox, etc.). You have to explicitly "open" the subfolder you need.
This isn't unique to tkinter either—you'll see this pattern across many Python libraries:
- If you use
from numpy import *, you won't get access tonumpy.linalgfunctions unless you dofrom numpy import linalg from pandas import *doesn't includepandas.plotting—you need to import that submodule separately to use its tools
3. A quick best practice note
As a side tip: from module import * is generally discouraged in larger projects because it clogs up your namespace with dozens of names you might not need, increasing the chance of accidental name collisions (e.g., if you define your own Button variable later). Most developers prefer cleaner, more explicit imports like:
import tkinter as tk from tkinter import ttk
This way, you clearly distinguish between standard tk widgets (tk.Button) and themed ttk widgets (ttk.Button), making your code easier to read and maintain.
内容的提问来源于stack exchange,提问作者PokeBros

