473,513 Members | 2,658 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Tkinter, StringVar and dict

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
4 5483
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
4694
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the instances: def initVariables(self): self.e =...
2
4610
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
1
2253
by: Club-B42 | last post by:
when i start opt_newlogin.py directly it works fine(outputs '1 1 1 1'), but if i start it from options.py there is an error(outputs ''). ======== opt_newlogin.py ======== from config import *...
0
1323
by: Michele Simionato | last post by:
I am in the process of learning pygtk and I would like to port some custom made Tkinter widgets to pygtk, just an exercise. For instance I have this code: .. from Tkinter import * .. .. class...
5
2033
by: max(01)* | last post by:
hello. the following code: 1 from Tkinter import * 2 3 class MiaApp: 4 def __init__(self, genitore): 5 self.mioGenitore = genitore 6 self.i = IntVar()
3
2216
by: William Gill | last post by:
Working with tkinter, I have a createWidgets() method in a class. Within createWidgets() I create several StringVars() and assign them to the textvariable option of several widgets. Effectively my...
2
1569
by: Chad | last post by:
Is there anyway to set the individual options in Tkinter to a particular variable. For example, I have a menu option(code is below) which has January, February, March and so on, which I would like...
8
2012
by: Lie | last post by:
Inspect the following code: --- start of code --- import Tkinter as Tk from Tkconstants import * root = Tk.Tk() e1 = Tk.Entry(root, text = 'Hello World') e2 = Tk.Entry(root, text = 'Hello...
3
4181
by: seanacais | last post by:
I'm trying to build an unknown number of repeating gui elements dynamically so I need to store the variables in a list of dictionaries. I understand that Scale "variable" name needs to be a...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.