473,372 Members | 953 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,372 software developers and data experts.

Confusing problem between Tkinter.Intvar() and self declared variable class

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():
obj = setattr( self.e, part, IntVar() )

That allowed me to save bundles of info without having to create
another large dictionary or list. I was using the variable in entry
boxes to store the amount of parts ordered:

Entry( cscGroup.interior(), width=3, textvariable =
getattr(self.e, part),
text=e.get()).grid(row=x, column=2, padx=4
)

However, I ran into problems when I tried to pickle the instances in
order to recall them later. To fix that problem I created my own
simple data class that allowed me to save the data the same way while
also having the ability to pickle it:

class DataVar:
def __init__(self):
self.data = 0
self.partName = ""

def set(self, value):
self.data = value

def get(self):
return self.data

But I just discovered another problem. It doesn't appear to hold data
the same way. The information appeared global when it was IntVar().
Now when I go outside the class that set up the entry boxes, the
information does not appear to be in DataVar. I print out the info the
following way:

def printValues(self):
for part, list in info.masterList.items():
e = getattr(self.e, part)
print str(part) + " --->" + str( e.get() )

This function is in the same class that initialized the DataVar
variables and also that called the class that setup the window to
enter the amount of parts. When I call that class I pass in the
variable in the following way:

spares = Spares(self.master, self.e)

So obviously there's something about Tkinter that causes the info to
be global. But even though the DataVar class is an independent class,
for some reason the information is not being maintained.

Does anyone have any idea why this is happening and how to fix it?

Thanks ahead of time,
Marc
Jul 18 '05 #1
2 4680
Marc wrote:
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():
obj = setattr( self.e, part, IntVar() )
It seems odd to use an IntVar as a container for IntVar instances.
obj will always be set to None; just use setattr().

That allowed me to save bundles of info without having to create
another large dictionary or list. I was using the variable in entry
A Python object is just a dictionary in disguise.
boxes to store the amount of parts ordered:

Entry( cscGroup.interior(), width=3, textvariable =
getattr(self.e, part),
text=e.get()).grid(row=x, column=2, padx=4
)

However, I ran into problems when I tried to pickle the instances in
order to recall them later. To fix that problem I created my own
simple data class that allowed me to save the data the same way while
also having the ability to pickle it:

class DataVar:
def __init__(self):
self.data = 0
self.partName = ""

def set(self, value):
self.data = value

def get(self):
return self.data

But I just discovered another problem. It doesn't appear to hold data
the same way. The information appeared global when it was IntVar().
Now when I go outside the class that set up the entry boxes, the
information does not appear to be in DataVar. I print out the info the
following way:

def printValues(self):
for part, list in info.masterList.items():
e = getattr(self.e, part)
print str(part) + " --->" + str( e.get() )

This function is in the same class that initialized the DataVar
variables and also that called the class that setup the window to
enter the amount of parts. When I call that class I pass in the
variable in the following way:

spares = Spares(self.master, self.e)

So obviously there's something about Tkinter that causes the info to
be global. But even though the DataVar class is an independent class,
for some reason the information is not being maintained.

Does anyone have any idea why this is happening and how to fix it?


I wasn't able to completely follow the last part of your post, so below is a
working version of what I /think/ you are trying to do. The important part
is a pickle-enhanced container for IntVars that stores their values instead
of the instances. It could easily be enhanced to support, say, StringVar by
inspecting the type of the values in the __setstate__() method.

<crocodile.py>
import Tkinter as tk
import pickle

class Namespace:
pass

class IntVars:
""" Every attribute is suposed to be a TKinter.IntVar instance """
def __getstate__(self):
d = dict(self.__dict__)
for k in d:
d[k] = d[k].get()
return d
def __setstate__(self, d):
for k, v in d.iteritems():
iv = tk.IntVar()
iv.set(v)
setattr(self, k, iv)

info = Namespace()

info.masterDict = {
"bird": 1,
"elephant": 2,
"crocodile": 3,
}

FILENAME = "crocodile.pickle"

class LoadError(Exception): pass

class Main(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
try:
self.loadVariables()
except LoadError:
self.initVariables()
self.saveVariables()
self.initControls()

def loadVariables(self):
try:
f = file(FILENAME)
except IOError:
raise LoadError
else:
# error handling is *incomplete*
try:
self.e = pickle.load(f)
finally:
f.close()

def saveVariables(self):
f = file(FILENAME, "wb")
try:
pickle.dump(self.e, f)
finally:
f.close()

def initVariables(self):
self.e = IntVars()
for part, lst in info.masterDict.iteritems():
iv = tk.IntVar()
iv.set(lst)
setattr(self.e, part, iv)

def initControls(self):
interior = self # cscGroup.interior()
x = 0
for part, lst in info.masterDict.iteritems():
e = tk.Entry(interior, width=3,
textvariable=getattr(self.e, part)) # text=... has no effect
e.grid(row=x, column=2, padx=4)
x += 1

self.button = tk.Button(self, text="Save values",
command=self.saveVariables)
self.button.grid(row=x, column=2, padx=4)

m = Main()
m.mainloop()
</crocodile.py>

Peter
Jul 18 '05 #2
Thanks Peter. That worked great! I was able to shoe horn that into my
main program with hardly any changes at all, and it was exactly what I
was looking for.

I was afraid my post was a little confusing, so I started another
thread on the general subject of Tk support for pickling. But
basically you answered both questions with one shot.

Thanks again,
Marc
Jul 18 '05 #3

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

Similar topics

0
by: max(01)* | last post by:
hello everybody. i am a a bit of a newbie in python/tkinter,and i am experimenting a bit with widgets like checkbuttons. in python, you can create a checkbutton instance like this: ........
6
by: max(01)* | last post by:
hi people. when i create a widget, such as a toplevel window, and then i destroy it, how can i test that it has been destroyed? the problem is that even after it has been destroyed, the instance...
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()
1
by: ab | last post by:
Hi, I have an editor(front end) that is written in Tcl/Tk. It has a menu bar, menu buttons, edit boxes, listboxes & comboboxes. I am invoking this editor from an application that is written in...
6
by: William Gill | last post by:
I am trying to get & set the properties of a widget's parent widget. What I have works, but seems like a long way around the block. First I get the widget name using w.winfo_parent(), then i...
11
by: William Gill | last post by:
I am placing radiobuttons in a 4 X 4 matrix (using loops) and keep references to them in a 2 dimensional list ( rBtns ). It works fine, and I can even make it so only one button per column can be...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
6
by: JyotiC | last post by:
hi, i am making a GUI using Tkinter, I have a button and a checkbutton. i want the button to be enable when checkbutton is on and disble when the checkbutton is off. thanx
1
kaarthikeyapreyan
by: kaarthikeyapreyan | last post by:
Beginners Game- Tic-Tac-Toe My first attempt after learning Tkinter from Tkinter import * import tkFont class myApp: """ Defining The Geometry of the GUI And the variables Used In the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.