diff --git a/baukit/labwidget.py b/baukit/labwidget.py index cbde03c..d541dc5 100644 --- a/baukit/labwidget.py +++ b/baukit/labwidget.py @@ -484,6 +484,16 @@ class Event(object): def __repr__(self): return f'Event({self.value}, {self.name})' + @property + def location(self): + ''' + Returns the location of the event, as translated by the + target object's event_location method, if any. + ''' + if self.target and hasattr(self.target, 'event_location'): + return self.target.event_location(self) + return None + entered_handler_stack = [] diff --git a/notebooks/using_show_and_widgets.ipynb b/notebooks/using_show_and_widgets.ipynb index 24c8db2..a29c0f4 100644 --- a/notebooks/using_show_and_widgets.ipynb +++ b/notebooks/using_show_and_widgets.ipynb @@ -493,7 +493,7 @@ }, { "cell_type": "markdown", - "id": "d58975ba", + "id": "0e1adc74", "metadata": {}, "source": [ "## Handling PlotWidget clicks\n", @@ -504,14 +504,14 @@ { "cell_type": "code", "execution_count": null, - "id": "ba3cbe25", + "id": "439c2809", "metadata": {}, "outputs": [], "source": [ "from baukit import Textbox\n", "coord_b = Textbox()\n", "def handle_click(e):\n", - " loc = pw.event_location(e)\n", + " loc = e.target.event_location(e)\n", " coord_b.value = f'x,y=({loc.x:.2f}, {loc.y:.2f}) inside={loc.inside}'\n", "pw.on('click', handle_click)\n", "show(show.TIGHT, [['Click the previous plot to see coordinates here:', coord_b]])"