0

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.

Jose Doval
  • 13
  • 3
  • Seems like the python version in video is different (probably older) than yours. Does this work for you: `from tkinter import *` `tkinter.colorchooser.askcolor()`? Take a look at https://docs.python.org/3.5/library/tkinter.html. – 0xc0de Jul 21 '15 at 06:25
  • Which video where you watching? – Anand S Kumar Jul 21 '15 at 06:57
  • the version on de video it is 3.3.3 and i usin 3.4.3, i tried if form tkinter import * tkinter.colorchooser.askcolor() and it doesn't work--> NameError: name 'tkinter' is not defined – Jose Doval Jul 21 '15 at 17:53
  • Also i realize that if I run the first one on the IDLE 3 it works . I understand all your answers but this confuses me xD. – Jose Doval Jul 21 '15 at 18:04

3 Answers3

1

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 *
adrianus
  • 3,141
  • 1
  • 22
  • 41
0

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.

Bogdan Nedelcu
  • 78
  • 1
  • 1
  • 5
0

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.

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176