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

为何从模块重复导入?关于tkinter导入方式的技术疑问

Why do people still import extra items after using 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 to numpy.linalg functions unless you do from numpy import linalg
  • from pandas import * doesn't include pandas.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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.21 06:44:09