[Tkinter-discuss] Re: Storing structured, 'widget-specific-data',
then loading it back in la ter
Fredrik Lundh
fredrik at pythonware.com
Sun Oct 3 08:42:09 CEST 2004
Chris Nethery wrote:
> self.testButton = Tkinter.Button(self.f, font=(('Verdana'), '7'),
> text='Test', command=self.getStoredData(self.widget))
to figure out why this doesn't work, consider what the following Python
constructs do:
getStoredData(widget)
self.getStoredData(self.widget)
variable = self.getStoredData(self.widget) # what ends up in variable?
function(keyword=self.getStoredData(self.widget)) # what's passed to the method?
Widget(command=self.getStoredData(self.widget)) # what's passed to the constructor?
(hint: the "getStoredData(...)" part does the same thing, and returns the same
thing, in all five cases)
to work around this, you can add a local function:
def my_callback(self=self):
self.getStoredData(self.widget)
self.testButton = Tkinter.Button(..., command=my_callback)
(this creates a callable object, my_callback, that has a reference to the "self"
instance variable. when the object is called, it will call the right getStoredData
method with the right instance variable)
you can also use a lambda, and in this case, you might not even need the
argument binding (at least not if you're using a recent version of Python):
... Button(..., command=lambda: self.getStoredData(self.widget))
(but don't add the workaround until you've figured out why the original code
doesn't work)
</F>
More information about the Tkinter-discuss
mailing list