By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,245 Members | 1,103 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,245 IT Pros & Developers. It's quick & easy.

Tkinter, StringVar and dict

P: n/a
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.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Dec 20 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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.

pyfrom Tkinter import *
pyL = Label()
pyL['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:

pyL['text'] = 'bob'
pyL.cget('text')
'bob'
pyL.config(text='carol')
pyL.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/
Dec 21 '06 #2

P: n/a
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

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/
Dec 21 '06 #3

P: n/a
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

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/
Dec 21 '06 #4

P: n/a
James Stroud wrote:
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

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.

--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Dec 22 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.