-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathsimple_event.py
More file actions
54 lines (36 loc) · 1.25 KB
/
simple_event.py
File metadata and controls
54 lines (36 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Simple Event
============
Example showing how to add a simple callback event.
"""
# test_example = false
# sphinx_gallery_pygfx_docs = 'screenshot'
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.loop.run()