.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "_gallery/misc/simple_event.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr__gallery_misc_simple_event.py: Simple Event ============ Example showing how to add a simple callback event. .. GENERATED FROM PYTHON SOURCE LINES 7-54 .. image-sg:: /_gallery/misc/images/sphx_glr_simple_event_001.webp :alt: simple event :srcset: /_gallery/misc/images/sphx_glr_simple_event_001.webp :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none {'value': 'viridis'} | .. code-block:: Python # test_example = false import fastplotlib as fpl import imageio.v3 as iio data = iio.imread("imageio:camera.png") # Create a figure figure = fpl.Figure(size=(700, 560)) # plot sine wave, use a single color image_graphic = figure[0,0].add_image(data=data) # show the plot figure.show() # define callback function to print the event data def callback_func(event_data): print(event_data.info) # Will print event data when the color changes image_graphic.add_event_handler(callback_func, "cmap") image_graphic.cmap = "viridis" # adding a click event, we can also use decorators to add event handlers @image_graphic.add_event_handler("click") def click_event(event_data): # get the click location in screen coordinates xy = (event_data.x, event_data.y) # map the screen coordinates to world coordinates xy = figure[0,0].map_screen_to_world(xy)[:-1] # print the click location print(xy) # NOTE: `if __name__ == "__main__"` is NOT how to use fastplotlib interactively # please see our docs for using fastplotlib interactively in ipython and jupyter if __name__ == "__main__": print(__doc__) fpl.run() .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.730 seconds) .. _sphx_glr_download__gallery_misc_simple_event.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: simple_event.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: simple_event.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: simple_event.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_