473,666 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(se lf, OPname, dname):

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

dname = dict((d,StringV ar()) 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\tk inter.py", line 165, in <module>
ps.initOPValues (c, OPValues[c])
File "C:\Code\gui\tk inter.py", line 47, i initOPValues
dname = dict((d,StringV ar()) for d in OPDefaults)
File "C:\Code\gui\tk inter.py", line 47, in <genexpr>
dname = dict((d,StringV ar()) for d in OPDefaults)
File "C:\Python25\li b\lib-tk\Tkinter.py", line 254, in __init__
Variable.__init __(self, master, value, name)
File "C:\Python25\li b\lib-tk\Tkinter.py", line 185, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
Exception exceptions.Attr ibuteError: "StringVar instance has no
attribute '_tk'"
in <bound method StringVar.__del __ of <Tkinter.String Var instance at
0x00B7D468>igno red

Any help is appreciated!

Thanks,

Kevin
Jun 27 '08 #1
3 4191
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(se lf, OPname, dname):

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

dname = dict((d,StringV ar()) 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\tk inter.py", line 165, in <module>
ps.initOPValues (c, OPValues[c])
File "C:\Code\gui\tk inter.py", line 47, i initOPValues
dname = dict((d,StringV ar()) for d in OPDefaults)
File "C:\Code\gui\tk inter.py", line 47, in <genexpr>
dname = dict((d,StringV ar()) for d in OPDefaults)
File "C:\Python25\li b\lib-tk\Tkinter.py", line 254, in __init__
Variable.__init __(self, master, value, name)
File "C:\Python25\li b\lib-tk\Tkinter.py", line 185, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
Exception exceptions.Attr ibuteError: "StringVar instance has no
attribute '_tk'"
in <bound method StringVar.__del __ of <Tkinter.String Var instance at
0x00B7D468>igno red

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(se lf, OPname, dname):

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

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

C:\Code\gui>tki nter.py
Traceback (most recent call last):
File "C:\Code\gui\tk inter.py", line 169, in <module>
ps.initOPValues (c, OPValues[c])
File "C:\Code\gui\tk inter.py", line 54, in initOPValues
dname = dict((d,tk.Stri ngVar()) for d in OPDefaults)
File "C:\Code\gui\tk inter.py", line 54, in <genexpr>
dname = dict((d,tk.Stri ngVar()) for d in OPDefaults)
File "C:\Python25\li b\lib-tk\Tkinter.py", line 254, in __init__
Variable.__init __(self, master, value, name)
File "C:\Python25\li b\lib-tk\Tkinter.py", line 185, in __init__
self._tk = master.tk
AttributeError: 'NoneType' object has no attribute 'tk'
Exception exceptions.Attr ibuteError: "StringVar instance has no
attribute '_tk'"
in <bound method StringVar.__del __ of <Tkinter.String Var instance at
0x00B7E418>igno red
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(se lf, OPname, dname):

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

dname = dict((d,tk.Stri ngVar()) 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
12540
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 greatly appreciated. -- here is snip of working code:
7
17957
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 each of the widgets using a temp variable to hold the text, but I don't wnat to reinvent the wheel if there already is something that does the job. Thanks, Bill
3
2224
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) ...
4
1568
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 widget each time so that it doesn't contain earlier ones? Actually, another question I have is, is there a way to set python so that it will assume any undefined variable is 0 or ''? That is, I have several statements like "If k 0 then so and so"...
10
1815
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): frame = Frame(master) frame.pack()
1
5106
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 class countdown(Tkinter.Frame):
4
2159
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: Google's great, but it has no truth meter. Do I inherit from Frame? Or is that a big mistake. (Both positions repeated frequently.) Do I use Tk() or toplevel()? (Support for both and if a cogent explanation of the differences exists, I didn't find it.)
3
3911
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 this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new window respectively having
0
1308
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 = Tix.Tk() myAppGUIObject = myAppGUI(myRoot)
0
8444
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
8356
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,...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8551
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,...
1
6198
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
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
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
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.