keypress event passing arguments

Hi all,
I have set up an event handler and function to perform actions when a user presses certain keys in the plot window. The function needs access to variables that are in the main function. How do I pass these variables to the function.

In OnKeyPress, ImageNumber and Li are objects in the main program. Any ideas on how to pass them? Thanks

plt.connect('key_press_event',OnKeyPress)

    def OnKeyPress(self,event):
         print 'button= ',event.key, event.xdata, event.ydata
         if event.key == 'm':
             ImageNumber = ImageNumber + 1
         if event.key == 'n':
             ImageNumber = ImageNumber - 1

         rawimage = Li.GetImage(ImageNumber)
         plt.imshow(rawimage)
         plt.title("Image number %3d" % (ImageNumber))
         plt.draw()

You can create an class to store these values with a method to handle the callback, eg. (untested code):

class KeyHandler:
     def __init__(self):
         self.ImageNumber = 0

     def OnKeyPress(self, event):
         self.ImageNumber += 1

key_handler = KeyHandler()

plt.connect('key_press_event', key_handler.OnKeyPress)

Mike

···

On 12/11/2010 07:24 PM, John wrote:

Hi all,
I have set up an event handler and function to perform actions when a
user presses certain keys in the plot window. The function needs access
to variables that are in the main function. How do I pass these
variables to the function.

In OnKeyPress, ImageNumber and Li are objects in the main program. Any
ideas on how to pass them? Thanks

plt.connect('key_press_event',OnKeyPress)

     def OnKeyPress(self,event):
          print 'button= ',event.key, event.xdata, event.ydata
          if event.key == 'm':
              ImageNumber = ImageNumber + 1
          if event.key == 'n':
              ImageNumber = ImageNumber - 1

          rawimage = Li.GetImage(ImageNumber)
          plt.imshow(rawimage)
          plt.title("Image number %3d" % (ImageNumber))
          plt.draw()

------------------------------------------------------------------------------
Oracle to DB2 Conversion Guide: Learn learn about native support for PL/SQL,
new data types, scalar functions, improved concurrency, built-in packages,
OCI, SQL*Plus, data movement tools, best practices and more.
http://p.sf.net/sfu/oracle-sfdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
matplotlib-users List Signup and Options
   
--
Michael Droettboom
Science Software Branch
Space Telescope Science Institute
Baltimore, Maryland, USA