Search code examples
pythonpython-3.xtkintersublimetext3sublimerepl

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.


Solution

  • 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 *