submodules
Peter Otten
__peter__ at web.de
Fri Mar 18 07:03:25 EDT 2016
ast wrote:
> Hello
>
> Since in python3 ttk is a submodule of tkinter, I was expecting this
> to work:
>
> from tkinter import *
>
> root = Tk()
> nb = ttk.Notebook(root)
>
> but it doesnt, ttk is not known.
>
> I have to explicitely import ttk with
>
> from tkinter import ttk
>
> why ?
If there's no tkinter.__all__
from tkinter import *
is basically
import tkinter
globals().update(
(name, value) for name, value in
vars(tkinter).items() if not name.startswith("_")
)
del tkinter
so if "from tkinter import *" doesn't inject ttk in your namespace this
means that there is no variable tkinter.ttk. This is because there is no
from . import ttk
in tkinter/__init__.py, and automagically importing all candidate submodules
is inefficient (and unsafe).
However, once you import tkinter.ttk the name is added
>>> import tkinter.ttk
>>> from tkinter import *
>>> ttk
<module 'tkinter.ttk' from '/usr/lib/python3.4/tkinter/ttk.py'>
so what you get with the *-import depends on what has been imported before,
perhaps by other modules.
Personally I'd avoid the *-import altogether...
More information about the Python-list
mailing list