[Tkinter-discuss] importing tk.W?

Michael O'Donnell michael.odonnell at uam.es
Tue Mar 11 07:55:51 CET 2014


Hi Monte,

  When you import all from tkinter, you get
a number of named constants as well as the classes
(constants being variables with a fixed value).

So, don't think of them as attributes, think of them
as variables, which tkinter defines like:

tk.W='w'
tk.BOTH='both'
etc.

Mick


On 11 March 2014 04:27, memilanuk <memilanuk at gmail.com> wrote:
> 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
> appreciated.
>
> 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!
>
> Thanks,
>
> Monte
>
> _______________________________________________
> Tkinter-discuss mailing list
> Tkinter-discuss at python.org
> https://mail.python.org/mailman/listinfo/tkinter-discuss


More information about the Tkinter-discuss mailing list