473,670 Members | 2,546 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.framewor k/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.framewor k/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py",
line 3237, in __setitem__
self.tk.call(se lf.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 5492
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(_f latten((self._w , cmd)) + self._options(c nf))
<class '_tkinter.TclEr ror'>: 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.framewor k/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.framewor k/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py",
line 3237, in __setitem__
self.tk.call(se lf.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.framewor k/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.framewor k/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py",
line 3237, in __setitem__
self.tk.call(se lf.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,StringV ar()) 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.framewor k/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.framewor k/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py",
line 3237, in __setitem__
self.tk.call(se lf.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,StringV ar(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.framewor k/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.framewor k/Versions/2.5/lib/python2.5/lib-tk/Tkinter.py",
line 3237, in __setitem__
self.tk.call(se lf.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,StringV ar()) 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
4704
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 = IntVar() for part, list in info.masterList.items():
2
4626
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 turn the contents of the 3rd list (call it states would be updated by a change in the 2nd list. If anyone can share a recipe or some ideas, I'd be grateful! Here's some sample code that displays three OptionMenus, but doesn't update the list...
1
2268
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 * from Tkinter import * from opt_newlogin import newlogin
0
1325
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 AnimatedLabel(Label): .. def __init__(self, master, text, width=72, maxspc=16, deltat=100, **kw): .. self.text = text
5
2051
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
2225
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 code structure is: def createWidgets(self): ... var = StringVar() Entry(master,textvariable=var) ...
2
1576
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 to have corresponding values of 01, 02, 03 and so on. Can someone please tell me how to do that within the context of the code I have below - or even totally modify it if you must. Label(self, text = "Month" ).grid(row = 5, column = 1,...
8
2018
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 World')
3
4193
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 StringVar but I cannot figure out how to initialize the dictionary. I've tried the following code ps = PowerSupply() # Instantiate a Power Supply VM Object numOPs = ps.getOnum() # Find the number of outputs OPValues = # Global list to...
0
8471
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8592
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7421
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6216
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5686
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4393
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2802
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 we have to send another system
2
1795
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.