Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #105195 > unrolled thread

submodules

Started by"ast" <nomail@com.invalid>
First post2016-03-18 11:42 +0100
Last post2016-03-18 13:44 +0100
Articles 3 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  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

#105195 — submodules

From"ast" <nomail@com.invalid>
Date2016-03-18 11:42 +0100
Subjectsubmodules
Message-ID<56ebdb83$0$3330$426a74cc@news.free.fr>
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 ?

[toc] | [next] | [standalone]


#105199

FromPeter Otten <__peter__@web.de>
Date2016-03-18 12:03 +0100
Message-ID<mailman.312.1458299016.12893.python-list@python.org>
In reply to#105195
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...

[toc] | [prev] | [next] | [standalone]


#105208

From"ast" <nomail@com.invalid>
Date2016-03-18 13:44 +0100
Message-ID<56ebf819$0$22767$426a74cc@news.free.fr>
In reply to#105199
"Peter Otten" <__peter__@web.de> a écrit dans le message de 
news:mailman.312.1458299016.12893.python-list@python.org...
> ast wrote:


ok, thx 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web