简体   繁体   中英

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. I am using sublime text 3 for wrinting code and the console of sublimeREPL to run the files and the code. 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.

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:

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

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 .

Packages are a way of structuring Python's module namespace by using “dotted module names”.

So the module tkinter is structured as follows:

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:

... the statement from sound.effects import * does not import all submodules from the package sound.effects into the current namespace

( source )

You can, however (even though many would say from ... import * is bad practice)

from tkinter.colorchooser import *

from tkinter import * doesn't work because you need the entire tkinter directory. This is why you must specificy colorchooser module for your program to work.

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 -

from tkinter import *

You have to specifically import it using -

from tkinter import colorchooser

As you already observed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM