Changing global variables in tkinter/pmw callback
Alex Martelli
aleaxit at yahoo.com
Fri Apr 6 11:13:23 EDT 2001
"Brian Elmegaard" <be at mek.dtu.dk> wrote in message
news:3ACDBA00.BB1B10D1 at mek.dtu.dk...
[about not _needing_ lambda]
> But (how) can the same be done with tkinter callbacks?
Just the same way: you can re-write the lambda, for clarity,
as a named local function, and pass that object.
> Take the apply example of mine. How would that look with def's?
>
> button.configure(command = lambda self=self,
> button=button, buttons=self.buttons,
> setting=CurrentCanvasSetting:
> self.apply(button,buttons,setting))
Which would become, to be literal in the transliteration:
def configure_command(self=self, button=button,
buttons=self.buttons, setting=CurrentCanvasSetting):
self.apply(button, buttons, setting)
button.configure(command = configure_command)
> In apply I (now) do:
> def apply(self,button,buttons,setting):
> setting[0]= button.cget('text')
That is another issue from 'avoiding lambda in favour of
named local functions' (which is strictly about clarity:
a local function may be clearer than a lambda, or,
according to personal taste, vice-versa).
Here, if I understand correctly, what you're trying to do
is somehow set an appropriate "global" string when a
function is executed. If that CurrentCanvasSetting is
a variable name in some module, and the module-object
is known to you as (e.g.) modobj, you could pass those
to your 'apply' function in some form or other:
def configure_command(self=self, button=button,
modobj=modobj, varname='CurrentCanvasSetting'):
self.apply(button, modobj, varname)
button.configure(command = configure_command)
and in the apply method:
def apply(self, button, modobj, varname):
setattr(modobj, varname, button.cget('text'))
> This long explanation really clarified things for me. The thing is that
> I have grown up with boxing languages, so post-it's, I now have to
Me too -- Fortran, Pascal, various machine-languages, lo that
many years ago. But I remember when the light first dawned on
me, about what was REALLY going on in my very first "post-it
language" (a LISP dialect), how it felt as if mists were
dissipating...!-)
Alex
More information about the Python-list
mailing list