timer_callback1.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Create a simple Play/Pause app with a timer event
  2. You can interact with the scene during the loop!
  3. ..press q to quit"""
  4. import time
  5. import numpy as np
  6. from vedo import Plotter
  7. from vedo.pyplot import plot
  8. def bfunc(obj, ename):
  9. global timer_id
  10. plotter.timer_callback("destroy", timer_id)
  11. if "Play" in button.status():
  12. # instruct to call handle_timer() every 10 msec:
  13. timer_id = plotter.timer_callback("create", dt=10)
  14. button.switch()
  15. def handle_timer(event):
  16. t = time.time() - t0
  17. x = np.linspace(t, t + 4*np.pi, 50)
  18. y = np.sin(x) * np.sin(x/12)
  19. fig = plot(
  20. x, y, '-o', ylim=(-1.2, 1.2), aspect=3/1,
  21. xtitle="time window [s]", ytitle="intensity [a.u.]",
  22. )
  23. fig.shift(-x[0]) # put the whole plot object back at (0,0)
  24. # Pop (remove) the old plot and add the new one
  25. plotter.pop().add(fig).render()
  26. timer_id = -1
  27. t0 = time.time()
  28. plotter= Plotter(size=(1200,600))
  29. # plt.initialize_interactor() # on windows this is needed
  30. button = plotter.add_button(bfunc, states=[" Play ","Pause"], size=40)
  31. evntid = plotter.add_callback("timer", handle_timer, enable_picking=False)
  32. x = np.linspace(0, 4*np.pi, 50)
  33. y = np.sin(x) * np.sin(x/12)
  34. fig = plot(x, y, ylim=(-1.2, 1.2), xtitle="time", aspect=3/1, lc='grey5')
  35. plotter.show(__doc__, fig, zoom=2)