Changeset 120


Ignore:
Timestamp:
12/08/04 18:10:09 (20 years ago)
Author:
cal103
Message:

Added function register() to handle mouse events.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/python/asaplot.py

    r119 r120  
    4747        self.figmgr = FigureManagerTkAgg(self.canvas, 1, self.window)
    4848        self.window.wm_title('ASAPlot graphics window')
     49
     50        self.events = {'button_press':None,
     51                       'button_release':None,
     52                       'motion_notify':None}
    4953
    5054        self.set_title(title)
     
    293297        """
    294298        self.window.destroy()
     299
     300
     301    def register(self, type=None, func=None):
     302        """
     303        Register, reregister, or deregister events of type 'button_press',
     304        'button_release', or 'motion_notify'.
     305       
     306        The specified callback function should have the following signature:
     307
     308            def func(event)
     309
     310        where event is an MplEvent instance containing the following data:
     311
     312            name                # Event name.
     313            canvas              # FigureCanvas instance generating the event.
     314            x      = None       # x position - pixels from left of canvas.
     315            y      = None       # y position - pixels from bottom of canvas.
     316            button = None       # Button pressed: None, 1, 2, 3.
     317            key    = None       # Key pressed: None, chr(range(255)), shift,
     318                                  win, or control
     319            inaxes = None       # Axes instance if cursor within axes.
     320            xdata  = None       # x world coordinate.
     321            ydata  = None       # y world coordinate.
     322
     323        For example:
     324
     325            def mouse_move(event):
     326                print event.xdata, event.ydata
     327
     328            a = asaplot()
     329            a.register('motion_notify', mouse_move)
     330
     331        If func is None, the event is deregistered.
     332
     333        Note that in TkAgg keyboard button presses don't generate an event.
     334        """
     335
     336        if not self.events.has_key(type): return
     337
     338        if func is None:
     339            if self.events[type] is not None:
     340                # It's not clear that this does anything.
     341                self.canvas.mpl_disconnect(self.events[type])
     342                self.events[type] = None
     343
     344                # It seems to be necessary to return events to the toolbar.
     345                if type == 'motion_notify':
     346                    self.canvas.mpl_connect(type + '_event',
     347                        self.figmgr.toolbar.mouse_move)
     348                elif type == 'button_press':
     349                    self.canvas.mpl_connect(type + '_event',
     350                        self.figmgr.toolbar.press)
     351                elif type == 'button_release':
     352                    self.canvas.mpl_connect(type + '_event',
     353                        self.figmgr.toolbar.release)
     354
     355        else:
     356            self.events[type] = self.canvas.mpl_connect(type + '_event', func)
    295357
    296358
     
    498560        if l:
    499561            if i is not None:
    500                 self.i = i
     562                self.i = i
    501563
    502564            if inc is not None:
    503                 self.i += inc
     565                self.i += inc
    504566
    505567            self.i %= l
Note: See TracChangeset for help on using the changeset viewer.