[Tkinter-discuss] importing tk.W?

memilanuk memilanuk at gmail.com
Tue Mar 11 04:27:18 CET 2014

So... this is kind of twisting my brain a bit.  Not sure I understand 
how/why this works the way it does.  Some help or explanation would be 

On the one hand, I keep reading about the evils/perils of wildcard 
imports and recommendations for named imports.  On the other hand, it 
seems like nearly every tkinter example in books, guides, tutorials that 
I see uses a wildcard import.

So I was trying to follow along some code examples from a recent book, 
and also trying to use python3 as much as practical.  Given that the 
original book code was python 2.x and I had to change 'Tkinter' to 
'tkinter', I figured why not change to using a named import, i.e.

import tkinter as tk

rather than

from Tkinter import *

Other than having to import a 'tk.' to widget names, I figured I should 
be good to go.  Unfortunately not.  The book had code like this:

from tkinter import *
root = Tk()
Label(root, text="Username").grid(row=0, sticky=W)

While what I was coming up with was more like this:

import tkinter as tk
root = tk.Tk()
tk.Label(root, text="Username").grid(row=0, sticky='W')

The difference being that unless I enclosed the W attribute for the 
option 'sticky=' in quotes, I got an error 'Unresolved reference 'W''.

I asked online, and found that apparently 'W' is imported along with 
'Label', 'Entry', 'Button', 'Tk', etc. at the top level of tkinter.  It 
appears I can use either a wildcard import, enclose it in quotes, or use 
explicit reference e.g. tk.W.

My question is this:  How / why does this make any sense, that an 
attribute (W) to an option (sticky=) would be imported at the same level 
as widgets themselves?  Is it some artifact left over from tcl, or just 
a bad idea that never went away?  Between having to explicitly reference 
every widget already (tk.Label) and having to be extra specific with 
options like sticky... I'm starting to see why everyone uses wildcard 
imports when it comes to tkinter!



More information about the Tkinter-discuss mailing list