473,287 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Using StringVars in List of Dictionaries as tkInter variables forScale and Label widgets

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 hold one dictionary per output

for c in range(numOPs):
OPValues.append({ })
ps.initOPValues(c, OPValues[c])

ps.initOPValues is defined as follows:

def initOPValues(self, OPname, dname):

OPDefaults = {
'Vval' : 0,
'Ival' : 0,
'Otemp' : 0
}

dname = dict((d,StringVar()) for d in OPDefaults)
for d in OPDefaults:
dname[d].set(OPDefaults[d])

I get the following error:

Traceback (most recent call last):
File "C:\Code\gui\tkinter.py", line 165, in <module>
ps.initOPValues(c, OPValues[c])
File "C:\Code\gui\tkinter.py", line 47, i initOPValues
dname = dict((d,StringVar()) for d in OPDefaults)
File "C:\Code\gui\tkinter.py", line 47, in <genexpr>
dname = dict((d,StringVar()) for d in OPDefaults)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 254, in __init__
Variable.__init__(self, master, value, name)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 185, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
Exception exceptions.AttributeError: "StringVar instance has no
attribute '_tk'"
in <bound method StringVar.__del__ of <Tkinter.StringVar instance at
0x00B7D468>ignored

Any help is appreciated!

Thanks,

Kevin
Jun 27 '08 #1
3 4144
seanacais wrote:
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 hold one dictionary per output

for c in range(numOPs):
OPValues.append({ })
ps.initOPValues(c, OPValues[c])

ps.initOPValues is defined as follows:

def initOPValues(self, OPname, dname):

OPDefaults = {
'Vval' : 0,
'Ival' : 0,
'Otemp' : 0
}

dname = dict((d,StringVar()) for d in OPDefaults)
for d in OPDefaults:
dname[d].set(OPDefaults[d])

I get the following error:

Traceback (most recent call last):
File "C:\Code\gui\tkinter.py", line 165, in <module>
ps.initOPValues(c, OPValues[c])
File "C:\Code\gui\tkinter.py", line 47, i initOPValues
dname = dict((d,StringVar()) for d in OPDefaults)
File "C:\Code\gui\tkinter.py", line 47, in <genexpr>
dname = dict((d,StringVar()) for d in OPDefaults)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 254, in __init__
Variable.__init__(self, master, value, name)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 185, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
Exception exceptions.AttributeError: "StringVar instance has no
attribute '_tk'"
in <bound method StringVar.__del__ of <Tkinter.StringVar instance at
0x00B7D468>ignored

Any help is appreciated!
There's some magic going on behind the scene which means that you have to
create a Tkinter.Tk instance before you can start churning out StringVars:
>>import Tkinter as tk
v = tk.StringVar()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 257, in __init__
Variable.__init__(self, master, value, name)
File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 188, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
>>root = tk.Tk()
v = tk.StringVar()
v.set(42)
v.get()
'42'

Peter

Jun 27 '08 #2
On May 19, 2:11 pm, Peter Otten <__pete...@web.dewrote:
>
There's some magic going on behind the scene which means that you have to
create a Tkinter.Tk instance before you can start churning out StringVars:
>import Tkinter as tk
v = tk.StringVar()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 257, in __init__
Variable.__init__(self, master, value, name)
File "/usr/lib/python2.5/lib-tk/Tkinter.py", line 188, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'>>root = tk.Tk()
>v = tk.StringVar()
v.set(42)
v.get()

'42'

Peter

I had the Tkinter import as

from Tkinter import * but I changed it to

import Tkinter as tk

and modified the creation of the root object to

root=tk.Tk()

I then had to change every instance of Menu, Label,
Button, and all Tkinter elements to be prefaced by
tk. (so Button became tk.Button). That kind of makes
sense to me.

However even after specifying StringVar is a tk type

def initOPValues(self, OPname, dname):

OPDefaults = {
'Vval' : 0,
'Ival' : 0,
'Otemp' : 0
}

dname = dict((d,tk.StringVar()) for d in OPDefaults)
for d in OPDefaults:
dname[d].set(OPDefaults[d])
I still get a very similar error message

C:\Code\gui>tkinter.py
Traceback (most recent call last):
File "C:\Code\gui\tkinter.py", line 169, in <module>
ps.initOPValues(c, OPValues[c])
File "C:\Code\gui\tkinter.py", line 54, in initOPValues
dname = dict((d,tk.StringVar()) for d in OPDefaults)
File "C:\Code\gui\tkinter.py", line 54, in <genexpr>
dname = dict((d,tk.StringVar()) for d in OPDefaults)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 254, in __init__
Variable.__init__(self, master, value, name)
File "C:\Python25\lib\lib-tk\Tkinter.py", line 185, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
Exception exceptions.AttributeError: "StringVar instance has no
attribute '_tk'"
in <bound method StringVar.__del__ of <Tkinter.StringVar instance at
0x00B7E418>ignored
Thanks!

Kevin
Jun 27 '08 #3
seanacais wrote:
I had the Tkinter import as

from Tkinter import * but I changed it to

import Tkinter as tk

and modified the creation of the root object to

root=tk.Tk()

I then had to change every instance of Menu, Label,
Button, and all Tkinter elements to be prefaced by
tk. (so Button became tk.Button). That kind of makes
sense to me.

However even after specifying StringVar is a tk type
You misunderstood. There is no such thing as a "tk type".
Whether you use

from Tkinter import *

or

import Tkinter as tk

is irrelevant for the problem at hand.
def initOPValues(self, OPname, dname):

OPDefaults = {
'Vval' : 0,
'Ival' : 0,
'Otemp' : 0
}

dname = dict((d,tk.StringVar()) for d in OPDefaults)
for d in OPDefaults:
dname[d].set(OPDefaults[d])
I still get a very similar error message
You have to ensure that the lines

from Tkinter import *
root = Tk()

are *executed* before the line

dname = dict((d, StringVar()) for d in OPDefaults)

While I prefer the alternative

import Tkinter as tk
root = tk.Tk()
# ...
dname = dict((d, tk.StringVar()) for d in OPDefaults)

this is just a matter of style.

Peter

PS: If you still can't fix your script, please post it completely or,
better, a small self-contained (runnable) script showing the same problem
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Adonis | last post by:
I wish to manually move widgets in Tkinter, now I have successfully done it, but with odd results, I would like to move the widgets with a much smoother manner, and better precision. Any help is...
7
by: William Gill | last post by:
Is there a simple way to cut and paste from a tkinter text widget to an entry widget? I know I could create a mouse button event that triggers a popup (message widget) prompting for cut/paste in...
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...
4
by: gmarkowsky | last post by:
Hi all, I'm trying to write a GUI that will put up multiple widgets in succession. My problem is that each widget also contains the previous widgets when they pop up. How do I reinitialize the...
10
by: TefJLives | last post by:
Hi all, I'm trying to write a GUI that will put up multiple widgets in succession, all using the same class. Here's the class for the widget: class TwoChoice: def __init__(self, master):...
1
by: vigacmoe | last post by:
Hi all, I'm trying to write a simple tkinter program, then this problem popped up. The followin code will describe the problem. ------------------------------------------ import Tkinter ...
4
by: MartinRinehart | last post by:
Everything I've read about Tkinter says you create your window and then call its mainloop() method. But that's not really true. This is enough to launch a default window from the console: ...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
0
by: dudeja.rajat | last post by:
On Mon, Aug 25, 2008 at 12:57 PM, <dudeja.rajat@gmail.comwrote: Ok...now I found the way to do that. But I'm stuck further. my code is as below: main module ********************** myRoot...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.