472,378 Members | 1,610 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,378 software developers and data experts.

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 5296
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
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
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
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
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
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
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
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
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
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
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.