Python import tkinter as tk 和 from tkinter import 之间的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15974787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between import tkinter as tk and from tkinter import
提问by Chris Aung
I know it is a stupid question but I am just starting to learn python and i don't have good knowledge of python. My question is what is the difference between
我知道这是一个愚蠢的问题,但我才刚刚开始学习 python 并且我对 python 没有很好的了解。我的问题是有什么区别
from Tkinter import *
and
和
import Tkinter as tk
?Why can't i just write
?为什么我不能写
import Tkinter
Could anyone spare a few mins to enlighten me?
谁能抽出几分钟来启发我?
采纳答案by ch3ka
from Tkinter import *imports every exposed object in Tkinter into your current namespace.
import Tkinterimports the "namespace" Tkinter in your namespace and
import Tkinter as tkdoes the same, but "renames" it locally to 'tk' to save you typing
from Tkinter import *将 Tkinter 中每个暴露的对象导入到您当前的命名空间中。
import Tkinter在您的命名空间中导入“命名空间”Tkinter 并
import Tkinter as tk执行相同的操作,但将其在本地“重命名”为“tk”以节省您的输入
let's say we have a module foo, containing the classes A, B, and C.
假设我们有一个模块 foo,包含类 A、B 和 C。
Then import foogives you access to foo.A, foo.B, and foo.C.
然后import foo让您访问 foo.A、foo.B 和 foo.C。
When you do import foo as xyou have access to those too, but under the names x.A, x.B, and x.C.
from foo import *will import A, B, and C directly in your current namespace, so you can access them with A, B, and C.
当您这样做时,您也import foo as x可以访问它们,但是在名称 xA、xB 和 xC 下
from foo import *将直接在您当前的命名空间中导入 A、B 和 C,因此您可以使用 A、B 和 C 访问它们。
There is also from foo import A, Cwich will import A and C, but not B into your current namespace.
还有from foo import A, Cwich 将导入 A 和 C,但不会将 B 导入您当前的命名空间。
You can also do from foo import B as Bar, which will make B available under the name Bar (in your current namespace).
您也可以执行from foo import B as Bar,这将使 B 在名称 Bar 下可用(在您当前的命名空间中)。
So generally: when you want only one object of a module, you do from module import objector from module import object as whatiwantittocall.
所以一般来说:当您只需要一个模块的一个对象时,您可以使用from module import objector from module import object as whatiwantittocall。
When you want some modules functionality, you do import module, or import module as shortnameto save you typing.
当您需要某些模块功能时,您可以这样做import module或import module as shortname以节省您的输入。
from module import *is discouraged, as you may accidentally shadow ("override") names, and may lose track which objects belong to wich module.
from module import *不鼓励这样做,因为您可能会不小心隐藏(“覆盖”)名称,并且可能会忘记哪些对象属于 wich 模块。
回答by NPE
You can certainly use
你当然可以使用
import Tkinter
However, if you do that, you'd have to prefix every single Tk class name you use with Tkinter..
但是,如果您这样做,则必须在您使用的每个 Tk 类名前加上Tkinter..
This is rather inconvenient.
这比较不方便。
On the other hand, the following:
另一方面,以下几点:
import Tkinter as tk
sidesteps the problem by only requiring you to type tk.instead of Tkinter..
通过只要求您输入tk.而不是Tkinter..
As to:
至于:
from Tkinter import *
it is generally a bad idea, for reasons discussed in Should wildcard import be avoided?
这通常是一个坏主意,原因在应避免通配符导入吗?
回答by The_Py_prog
Writing:
写作:
from tkinter import *
results in importing everything that exists in tkintermodule
导致导入tkinter模块中存在的所有内容
Writing:
写作:
import tkinter
results in importing tkintermodule but if you do so, in order to be able to call any method you will have to use:
导致导入tkinter模块,但如果你这样做,为了能够调用任何方法,你将不得不使用:
tkinter.function_name()

