[英]i need to know whats the diference between from tkinter import * and from tkinter import 'module'
I am learnig python for the beginning, I am doing some tutorials and video-tutorials. 一开始我是学习python的人,正在做一些教程和视频教程。 I am using sublime text 3 for wrinting code and the console of sublimeREPL to run the files and the code.
我正在使用sublime text 3编写代码,并使用sublimeREPL控制台来运行文件和代码。 a few days ago I had to search on the web how to make sublimeREPL to run as IDLE 3 runs, and i made it.
几天前,我不得不在网上搜索如何使sublimeREPL在IDLE 3运行时运行,而我做到了。
The problem right now is that in the lesson i am doing right now they are teaching me how to use tkinter but in the videos the guy codes: 现在的问题是,我现在正在上课,他们在教我如何使用tkinter,但是在视频中,家伙代码是:
from tkinter import *
colorchooser.askcolor()
and it works, but when i code that , it doesn't work. 它可以工作,但是当我编写代码时,它不起作用。 the error says:
错误说:
Traceback (most recent call last): File "", line 1, in NameError: name 'colorchooser' is not defined
追溯(最近一次通话最近):NameError中文件“”的第1行:未定义名称“ colorchooser”
I need to code : 我需要编码:
from tkinter import colorchooser
colorchooser.askcolor()
and it works. 而且有效。
I just need to know why do I have to do it like this?, and why doesn't it work for me in the first way? 我只需要知道为什么我必须这样做?为什么它对我不起作用?
I not a English Speaker I tried my best. 我不是说英语的人,我尽力了。
With 用
from tkinter import colorchooser
you are importing the (sub-) module colorchooser
(plus its variables and functions) from the package (which is a structured module) tkinter
. 要导入的(子) 模块
colorchooser
(加上其变量和函数)从包装 (这是一个结构化的模块) tkinter
。
Packages are a way of structuring Python's module namespace by using “dotted module names”.
包是一种通过使用“点分模块名称”来构造Python模块名称空间的方法。
So the module tkinter
is structured as follows: 因此,模块
tkinter
的结构如下:
tkinter/
colorchooser/
askcolor()
...
With from tkinter import *
you are importing all methods and variables from tkinter
(in fact all public objects not starting with _
), but not its submodules , this is why the two methods are not the same: 随着
from tkinter import *
您是进口的所有方法和变量从tkinter
(其实所有的公共对象不开始_
), 而不是它的子模块 ,这就是为什么这两种方法是不一样的:
... the statement
from sound.effects import *
does not import all submodules from the package sound.effects into the current namespace...
from sound.effects import *
中的语句不会将sound.effects包中的所有子模块导入当前名称空间
You can, however (even though many would say from ... import *
is bad practice) 您可以,但是(尽管很多人会说
from ... import *
是不好的做法)
from tkinter.colorchooser import *
from tkinter import * doesn't work because you need the entire tkinter directory. 从tkinter import *不起作用,因为您需要整个tkinter目录。 This is why you must specificy colorchooser module for your program to work.
这就是为什么必须指定colorchooser模块才能使程序正常工作的原因。
I am not sure where you saw that , but colorchooser
has always been a module inside tkinter
and it does not get imported when you do - 我不知道你在哪里看到了这一点,但
colorchooser
一直是内部模块tkinter
它当你没有得到进口-
from tkinter import *
You have to specifically import it using - 您必须使用-专门导入它
from tkinter import colorchooser
As you already observed. 正如您已经观察到的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.