简体   繁体   English

我需要知道tkinter import *和tkinter import'module'之间的区别是什么

[英]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包中的所有子模块导入当前名称空间

( source ) 来源

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.

相关问题 从Tkinter导入* ImportError:没有名为“ Tkinter”的模块 - from Tkinter import * ImportError: No module named 'Tkinter' 导入tkinter与tk和tkinter导入之间的区别 - Difference between import tkinter as tk and from tkinter import 是否有 function 用于使用 from tkinter import * 模块重新定位 tkinter window? - Is there a function for repositioning the tkinter window using the from tkinter import * module? 为什么我需要从 tkinter 模块显式导入字体模块,即使使用“*”导入了完整模块? - Why do I need to explicitly import the font module from the tkinter module even if have imported the full module using “*”? Tkinter导入和从 - Tkinter import and from 如何使用 ```import Tkinter``` 而不是 ```from tkinter import *``` 设置 Tkinter window 的大小? - How can I set a size for a Tkinter window using ```import Tkinter``` instead of ```from tkinter import *```? python从tkinter导入ttk和* - python import ttk and * from tkinter 为什么 from tkinter import * 不导入 Tkinter 的消息框? - Why does from tkinter import * not import Tkinter's messagebox? 将用户输入从TKinter按钮导入到另一个.py模块 - import user input from TKinter button to a different .py module 导入错误:无法从部分初始化的模块“tkinter”导入名称“Frame” - ImportError: cannot import name 'Frame' from partially initialized module 'tkinter'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM