Tkinter, StringVar and dict
James Stroud
jstroud at mbi.ucla.edu
Wed Dec 20 19:50:22 EST 2006
Kevin Walzer wrote:
> I'm trying to manage user preferences in a Tkinter application by
> initializing some values that can then be configured from a GUI. The
> values are set up as a dict, like so:
>
> self.prefs= {
> 'interface': '-en1',
> 'verbose': '-v',
> 'fontname': 'Courier',
> 'point': 12,
> }
>
> To link these values to the appropriate Tkinter variables, I'm using
> code like this:
>
> self.prefs['interface'] = StringVar()
> self.prefs['interface'].set("-en0") # initialize
Are you sure this particular "prefs" is a dictionary and not some kind
of Tkinter entity, like, say, a Toplevel?
E.g.
py> from Tkinter import *
py> L = Label()
py> L['prefs'] = 'avalue'
------------------------------------------------------------
Traceback (most recent call last):
File "<ipython console>", line 1, in <module>
File
"/data10/users/jstroud/Programs/lib/python2.5/lib-tk/Tkinter.py", line
1204, in __setitem__
self.configure({key: value})
File
"/data10/users/jstroud/Programs/lib/python2.5/lib-tk/Tkinter.py", line
1197, in configure
return self._configure('configure', cnf, kw)
File
"/data10/users/jstroud/Programs/lib/python2.5/lib-tk/Tkinter.py", line
1188, in _configure
self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
<class '_tkinter.TclError'>: unknown option "-prefs"
Does this error message look familiar?
Consider:
py> L['text'] = 'bob'
py> L.cget('text')
'bob'
py> L.config(text='carol')
py> L.cget('text')
'carol'
James
> This raises an error in Tkinter:
>
> Exception in Tkinter callback
> Traceback (most recent call last):
> File
> "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py",
> line 1403, in __call__
> return self.func(*args)
> File "/Users/kevin/Programming/packetstream/packetstream-classes.py",
> line 293, in setPrefs
> self.prefs['interface'] = StringVar()
> File
> "/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py",
> line 3237, in __setitem__
> self.tk.call(self.name, 'configure', '-'+key, value)
> TclError: unknown option "-interface"
>
> Can someone help me smooth this out--to get dict key-values into a
> Tkinter variable like StringVar()?
>
> Thanks.
>
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com/
More information about the Python-list
mailing list