Tkinter, StringVar and dict

K

Kevin Walzer

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

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.
 
J

James Stroud

Kevin said:
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/
 
J

James Stroud

Kevin said:
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

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.

I overlooked this latter question.

Probably, your naming confusion above with self.prefs and the resulting
errors obscure your intention. But, were I to keep a dictionary of
StringVars for prefs, I would initialize it in this manner:

# somewhere in self
defaults = {
'interface' : '-en1',
'verbose' : '-v',
'fontname' : 'Courier',
'point' : 12
}
self.prefs = dict((d,StringVar()) for d in defaults)
for d in defaults:
self.prefs[d].set(defaults[d])

Note, of course, that you will need to access 'point' like this if you
want it back as an int:

int(self.prefs['point'].get())

Because StringVars return strings from their get() method.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
J

James Stroud

Kevin said:
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

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.

Actually, even more succinctly:

# somewhere in self
defaults = {
'interface' : '-en1',
'verbose' : '-v',
'fontname' : 'Courier',
'point' : 12
}
self.prefs = dict((d,StringVar(value=v)) for (d,v) in defaults.items())

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
 
K

Kevin Walzer

James said:
Kevin said:
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

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.

I overlooked this latter question.

Probably, your naming confusion above with self.prefs and the resulting
errors obscure your intention. But, were I to keep a dictionary of
StringVars for prefs, I would initialize it in this manner:

# somewhere in self
defaults = {
'interface' : '-en1',
'verbose' : '-v',
'fontname' : 'Courier',
'point' : 12
}
self.prefs = dict((d,StringVar()) for d in defaults)
for d in defaults:
self.prefs[d].set(defaults[d])

Note, of course, that you will need to access 'point' like this if you
want it back as an int:

int(self.prefs['point'].get())

Because StringVars return strings from their get() method.

James
Thanks, these snippets helped me work this out.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top