[Tkinter-discuss] confused on grid positioning vs. imports

Monte Milanuk memilanuk at gmail.com
Fri Mar 8 06:38:52 CET 2013


So... I've been tinkering with python and Tkinter and ttk a bit lately, 
and because I've been using pylint which complains a lot if I do a 
wildcard import like I see in many (most?) Tkinter tutorials i.e.

from Tkinter import *
from ttk import *

I've been trying to avoid doing wildcare imports, but I was finding that 
if I imported in this form:

from Tkinter import Tk
from ttk import Frame, Label, Button, Entry

...that I started running into problems where my grid positioning info 
started causing warnings that the variables 'S' and 'E' were not 
defined.  Here is an example line:

labelCel = Label(frame, text="Celsius temperature:")
labelCel.grid(row=1, column=1, sticky=(S, E))
labelFar = Label(frame, text="Fahrenheit temperature:")
labelFar.grid(row=2, column=1, sticky=(S, E))

Furthermore, when I did try to run the program, the python interpreter 
objected as well and it crashed at the first undefined 'S'...

I dug around a little and found one resource that instead imported like 
this:

import Tkinter as tk

and then the code would look like this:

labelCel = tk.Label(frame, text="Celsius temperature:")
labelCel.grid(row=1, column=1, sticky=(tk.S, tk.E))
labelFar = tk.Label(frame, text="Fahrenheit temperature:")
labelFar.grid(row=2, column=1, sticky=(tk.S, tk.E))

Which worked and is all well and good... if I didn't want ttk widgets 
instead of Tkinter when possible.

Somewhere along the line I found yet another example/tutorial that 
showed something like this:

from Tkinter import Tk, S, E
from ttk import Frame, Label, Button, Entry

which works with the original code example, but thats where I get 
confused.

I have to import Label from ttk but the sticky parameters from Tkinter? 
  Pardon me for saying, but that doesn't appear to make a whole lot of 
sense at first glance.  Am I getting that correct, or is there some 
other way to do it that is more correct?

I'm starting to think it'd just be easier to put some tags in comments 
at the start of the file to tell pylint to disable warning me about 
wildcard imports, and just go back to using those!

Thanks,

Monte



More information about the Tkinter-discuss mailing list