[GRASSGUI] Re: python question - waiting for a variable to change

Daniel Calvelo dca.gis at gmail.com
Mon Mar 19 17:47:15 EDT 2007


On 3/19/07, Michael Barton <michael.barton at asu.edu> wrote:
> Daniel,
>
> Please excuse my novice ignorance. I guess I need a bit of clarification.

Oh, I' m sorry I'm being unclear...

> >> Is your response below related to such a work flow?
> >
> > Yes. The idea is:
> >
> >   module A calls module B *and tells it what to do when closing*
> >
> > technically, you would have in A
> >
> > def do_the_rest( the_value_we_need ):
> >   do_it( the_value_we_need )
> >
> > moduleB.startplease( when_finished = do_the_rest )
>
> I understand this and can add it to the LayerTree class in gismutils
>
> >
> > and module B has to a) accept this when_finished parameter in the
> > startplease method and b) call it within the button pressed event. Stg
> > like:
>
> This is where I'm more fuzzy.
>
> >
> > def startplease( self, when_finished=None):
> >   self.onRunHook = when_finished
> >   ...
> >
>
> Is self.onRunHook an event handler method or a variable? Is it built in or
> something I'd need to create?

onRunHook as defined above is a variable holding a function. When you
call startplease() you pass it a when_finished parameter (the name is
arbitrary, of course) whose value is a function waiting to be called
with one argument. You then attach this function to self, which is the
only object you may carry through event handlers.

> This (and the button) are in module B. I don't understand what
> self.onRunHook() is doing.
>
> > def onRun(self, event):
> >   ...
> >   if self.onRunHook is not None:
> >     self.onRunHook() # run it
> >   ...

Here you are in the event handler. You have "armed" self with a
function that has been defined by the calling module and attached to
the object by the startplease method. If it is set (is not None, which
is the default argument to the startplease method), then do call it.

> Sorry to be so dense.
>
> Michael

Don't, please. My fault for being sketchy.

It is one of python's strenghts to treat functions, methods, even
classes as things that can be carried around with variables. You can
do stuff like:

def print_one():
  print "one"

def print_two():
  print "two"

which_one={1:print_one, 2:print_two}

which_one[ 1 ]() # call print_one, the value of which_one[1]

This is perfect for event-driven programs, where the flow of the
program is determined by what events occur in the interface. I'm sorry
I'm not fluent in Tcl/Tk to give you the corresponding equivalents.
Maybe somebody else is.

Daniel.

-- 
-- Daniel Calvelo Aros




More information about the grass-gui mailing list