Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105199
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: submodules |
| Date | 2016-03-18 12:03 +0100 |
| Organization | None |
| Message-ID | <mailman.312.1458299016.12893.python-list@python.org> (permalink) |
| References | <56ebdb83$0$3330$426a74cc@news.free.fr> |
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...
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
submodules "ast" <nomail@com.invalid> - 2016-03-18 11:42 +0100
Re: submodules Peter Otten <__peter__@web.de> - 2016-03-18 12:03 +0100
Re: submodules "ast" <nomail@com.invalid> - 2016-03-18 13:44 +0100
csiph-web