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

2008-05-19 Thread seanacais
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 
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 
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 > ignored

Any help is appreciated!

Thanks,

Kevin
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using StringVars in List of Dictionaries as tkInter variables for Scale and Label widgets

2008-05-19 Thread seanacais
On May 19, 2:11 pm, Peter Otten <[EMAIL PROTECTED]> wrote:
>
> 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 "", line 1, in 
>   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 
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 
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 > ignored


Thanks!

Kevin
--
http://mail.python.org/mailman/listinfo/python-list


Re: Using StringVars in List of Dictionaries as tkInter variables for Scale and Label widgets

2008-05-20 Thread seanacais
On May 20, 2:40 am, Peter Otten <[EMAIL PROTECTED]> wrote:
> seanacais wrote:
>
> 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)
>
> 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

I had looked for that, but didn't see it.  After reading your post I
went back
to look again.  Indeed, the initOPValues() function was being called
before
the root=Tk().  I've fixed the order of the calls and my script is
behaving
exactly as I expect now.  Thank you for taking the time to help me.

Kevin
--
http://mail.python.org/mailman/listinfo/python-list


finding the parent class (not superclass) of the currently executing method derived from a Borg class

2008-09-08 Thread seanacais
I want to create a class derived from a Borg class that can
instantiated as part of a script or be contained in other classes.
When methods from the Borg class are called, I would  like to know the
name of the class that contains the Borg class.

I've played a bit with inspect and _getframe from the sys module but
get inconsistent results.  The main problem is if the Borg class is
instantiated outside a containing class, then I need to go up a
different number of stack frames.  But this information isn't
available till after I've run out of stack frames.

Hopefully the following code better describes what I'm looking to do.

import sys

class Borg:
_shared_state = {}
def __init__(self):
self.__dict__=self._shared_state

class Assimilated(Borg):
valueByCaller = {}

def __init__(self, setupvalue):
print "In Assimilated.__init__()"
print "setupvalue is: " + str(setupvalue)

# would like  key to  be name of class (or module) that
# contins Assimilated
callerID =  sys._getframe(1).f_code.co_name

self.valueByCaller[callerID] =  setupvalue

print self.valueByCaller

def action(self, calledvalue):
print "In Assimilated.action()"
print "self.__classname__: " + self.__class__.__name__
print "calledvalue is: " + str(calledvalue)

print "self.valueByCaller"
print self.valueByCaller

# need to get proper key depending on which class (or module)
# made the call
#print "0: " + sys._getframe(0).f_code.co_name
#print "1: " + sys._getframe(1).f_code.co_name
#print "2: " + sys._getframe(2).f_code.co_name
#print "3: " + sys._getframe(3).f_code.co_name
callerID =  sys._getframe(2).f_code.co_name
print "callerID"
print callerID

if(self.valueByCaller[callerID] <= calledvalue):
print "doing the action"


class A:
assim_object = Assimilated(2)

def __init__(self):
self.assim_object.action(2)
self.assim_object.action(3)

class B:
assim_object = Assimilated(3)

def __init__(self):
self.assim_object.action(3)
self.assim_object.action(4)

class C:
assim_object = Assimilated(4)

def __init__(self):
self.assim_object.action(4)
self.assim_object.action(5)


a=A()
b=B()
c=C()

obj=Assimilated(3)
#obj.action(3)


When I run this, I get the following output:

In Assimilated.__init__()
setupvalue is: 2
{'A': 2}
In Assimilated.__init__()
setupvalue is: 3
{'A': 2, 'B': 3}
In Assimilated.__init__()
setupvalue is: 4
{'A': 2, 'C': 4, 'B': 3}
In Assimilated.action()
self.__classname__: Assimilated
calledvalue is: 2
self.valueByCaller
{'A': 2, 'C': 4, 'B': 3}
callerID

Traceback (most recent call last):
  File "\CallerID.py", line 67, in 
a=A()
  File "\CallerID.py", line 49, in __init__
self.assim_object.action(2)
  File "\CallerID.py", line 41, in action
if(self.valueByCaller[callerID] <= calledvalue):
KeyError: ''

What I found most peculiar when I started this was that the
valueByCaller dictionary was completely populated before the __init__
method of a was executed.  I'm pretty sure that this has to do with
the difference between when the object gets instanced and when it gets
initialized, but I need to do some more research and reading to be
able to explain it to myself.

Thanks for any help you can give me.

Kevin
--
http://mail.python.org/mailman/listinfo/python-list


variables of the class are not available as default values?

2009-08-27 Thread seanacais
I'm working on a program where I wish to define the default value of a
method as a value that was set in __init__.  I get a compilation error
saying that self is undefined.

As always a code snippet helps :-)

class foo:
def __init__(self, maxvalue):
self.maxvalue = maxvalue
self.value = 0

def put(self, value=self.maxvalue):
self.value = value

So if I call foo.put() the value is set to maxvalue but maxvalue can
be specified when I instantiate foo.

Explanations and/or workarounds much appreciated.



python test.py
Traceback (most recent call last):
  File "test.py", line 1, in 
class foo:
  File "test.py", line 6, in foo
def put(self, value=self.maxvalue):
NameError: name 'self' is not defined
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: variables of the class are not available as default values?

2009-08-27 Thread seanacais
On Aug 27, 5:44 pm, Chris Rebert  wrote:
> On Thu, Aug 27, 2009 at 2:37 PM, seanacais wrote:
> > I'm working on a program where I wish to define the default value of a
> > method as a value that was set in __init__.  I get a compilation error
> > saying that self is undefined.
>
> > As always a code snippet helps :-)
>
> > class foo:
> >    def __init__(self, maxvalue):
> >        self.maxvalue = maxvalue
> >        self.value = 0
>
> >    def put(self, value=self.maxvalue):
> >        self.value = value
>
> > So if I call foo.put() the value is set to maxvalue but maxvalue can
> > be specified when I instantiate foo.
> > python test.py
> > Traceback (most recent call last):
> >  File "test.py", line 1, in 
> >    class foo:
> >  File "test.py", line 6, in foo
> >    def put(self, value=self.maxvalue):
> > NameError: name 'self' is not defined
> > Explanations and/or workarounds much appreciated.
>
> Workaround:
>
> class foo:
>     def __init__(self, maxvalue):
>         self.maxvalue = maxvalue
>         self.value = 0
>
>     def put(self, value=None):
>         self.value = self.value if value is None else value
>
> Explanation:
>
> Default values are only evaluated once, when the class is defined,
> thus "self" is not defined at that point since the class is still
> being defined when the method definition is executed and thus there
> can be no instances yet anyway.
>
> Cheers,
> Chris
> --http://blog.rebertia.com

That clears that up for me.   Thank you!
-- 
http://mail.python.org/mailman/listinfo/python-list