为什么使用 Python 代码“from tkinter import ttk"?

编程入门 行业动态 更新时间:2024-10-27 16:32:59
本文介绍了为什么使用 Python 代码“from tkinter import ttk"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-30 07:51:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1391441.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:代码   tkinter   Python   quot   ttk

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!