问题描述
限时送ChatGPT账号..使用 TK GUI 的 Python 应用程序通常调用以下导入语句:
Python applications using TK GUIs usually call the following import statements:
from tkinter import *
from tkinter import ttk
调用 from tkinter import ttk
不是多余的吗?我认为 ttk 已经通过调用 from tkinter import *
Isn't calling from tkinter import ttk
redundant? I thought ttk would already be imported by calling from tkinter import *
请解释为什么需要单独导入ttk?
Please explain why ttk needs to be imported separately?
(我已经知道它有改进的小部件".我想了解为什么无法从 from tkinter import *
调用访问改进的小部件.)
(I already understand that it has "improved widgets". I want to understand why the improved widgets aren't accessible from the from tkinter import *
call.)
推荐答案
如果你去你的 python 安装位置,你会发现在 python 库中,tkinter 是一个文件夹,而不是一个 .py 文件.所以当你使用 from tkinter import *
时,你实际上只在 your-python-location/lib/tkinter/__init__.py
中导入了东西.ttk 之类的东西实际上是 tkinter 文件夹中的单独文件(例如 lib/tkinter/ttk.py、lib/tkinter/scrolledtext.py 等)因此,from tkinter import *
和 from tkinter importtkk
是从不同模块导入东西的不同命令.
If you go to your python installed location, you will find that in the python library, tkinter is a folder instead of a .py file. So when you use from tkinter import *
, you actually only imported things in your-python-location/lib/tkinter/__init__.py
. Things like ttk are actually separate files in tkinter folder(e.g. lib/tkinter/ttk.py, lib/tkinter/scrolledtext.py etc.) Therefore, from tkinter import *
and from tkinter import tkk
are different commands that import things from different module.
这篇关于为什么使用 Python 代码“from tkinter import ttk"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论