Using StringVars in List of Dictionaries as tkInter variables for Scale and Label widgets

Peter Otten __peter__ at web.de
Tue May 20 02:40:06 EDT 2008


seanacais wrote:

> I had the Tkinter import as
> 
> from Tkinter import * but I changed it to
> 
> import Tkinter as tk
> 
> and modified the creation of the root object to
> 
> root=tk.Tk()
> 
> I then had to change every instance of Menu, Label,
> Button, and all Tkinter elements to be prefaced by
> tk.  (so Button became tk.Button).  That kind of makes
> sense to me.
> 
> However even after specifying StringVar is a tk type

You misunderstood. There is no such thing as a "tk type". 
Whether you use 

from Tkinter import * 

or

import Tkinter as tk

is irrelevant for the problem at hand.
 
> def initOPValues(self, OPname, dname):
> 
> OPDefaults = {
> 'Vval'  : 0,
> 'Ival'  : 0,
> 'Otemp' : 0
> }
> 
> dname = dict((d,tk.StringVar()) for d in OPDefaults)
> for d in OPDefaults:
> dname[d].set(OPDefaults[d])
> 
> 
> I still get a very similar error message

You have to ensure that the lines

from Tkinter import *
root = Tk()

are *executed* before the line

dname = dict((d, StringVar()) for d in OPDefaults)

While I prefer the alternative 

import Tkinter as tk
root = tk.Tk()
# ...
dname = dict((d, tk.StringVar()) for d in OPDefaults)

this is just a matter of style.

Peter

PS: If you still can't fix your script, please post it completely or,
better, a small self-contained (runnable) script showing the same problem



More information about the Python-list mailing list