[Tutor] If not global, how?

alan.gauld@bt.com alan.gauld@bt.com
Tue Apr 1 07:46:01 2003

> class EntryClass(Pmw.EntryField):
> 	def __init__(self, parent=0):
> 	def SendCommand(command):
> 		for command in ('\x09','\x06'):
>                 ....
> 			old=outputbox.getvalue()
>                 ....
> 	def Callback(self):
> 		value=self.getvalue()
> 		if value: SendCommand(value)

First of all you show sendCommand as part of the class (by the
indentation) but it doesn't have a self parameter. It does have 
a command parameter so python will think that 'command' is the 
self value(self is just a naming convention, you can call the 
first paramerter in a method anything you like!). I don't think 
that's what you want, rather you want to put sendCommand function 
outside the class.

Second within sendCommand you refer to outputbox - where is this 
defined? Its not part of the Entry class not in sendCommand itself?
I suspect you want to pass outputbox into sendCommamd as an argument.

If you move sendCommand out of the class and add the outputbox as a 
parameter then your Callback should work fine since it will see 
sendCommand as a global function - although you need to find a way 
for sendCommand to locate the right outputbox!.

Without really knowing what your GUI design is like its kind of 
hard to know the solution. I'm inclined to go back to first 
principles and ask what the responsibility of your Entryclass is? 
Normally an Entry Class stores and retrieves text. Thats it. 
End of story. Now you are subclassing the Entry to add what 
capability? What will be different about your EntryClass from 
the standard one?

If you are trying to link the entryClass to sending (or receiving)
data along a port then maybe its a PortEntry class and it should 
have a reference to the port in the class. But if thats the case 
then it seems likely the outputbox needs to be involved somewhere 

So, is the EntryClass tied to a single specific outputbox? If so 
add a reference to it in your class and pass it in as an argument 
to init(). You can then pass it out to sendCommand() from your 
callback. If OTOH there are multiple output boxes and ports then 
it gets much more complicated and I think we need to step back 
and look at the bigger picture of whats required.

Alan g.
Author of the Learn to Program website