timer_callback3.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Create 2 independent timer callbacks:"""
  2. from vedo import *
  3. # Defining a function to be called by a timer event
  4. def func1(event):
  5. # Check if this function was called by the right timer
  6. if event.timerid != ida:
  7. return
  8. # Rotate a cube mesh and set its color to green5
  9. msh.rotate_z(1.0).c("green5")
  10. # Update text and print a message with the event and timer ids
  11. txt.text("func1() called").background('green5')
  12. printc(f"func1() id={event.id}, timerid={event.timerid}", c='g')
  13. plt.render()
  14. # Defining another function to be called by a different timer event
  15. def func2(event):
  16. # Check if this function was called by the right timer
  17. if event.timerid != idb:
  18. return
  19. # Rotate the same cube mesh in a different direction
  20. msh.rotate_x(5.0).c("red5")
  21. # Update text and print a message with the event and timer ids
  22. txt.text("func2() called").background('red5')
  23. printc(f"func2() id={event.id}, timerid={event.timerid}", c='r')
  24. plt.render()
  25. # Create a cube mesh and a text object
  26. msh = Cube()
  27. txt = Text2D(font="Calco", pos='top-right')
  28. # Create a plotter object with axes
  29. plt = Plotter(axes=1)
  30. # plt.initialize_interactor() # on windows this is needed
  31. # Add the two callback functions to the plotter's timer events
  32. id1 = plt.add_callback("timer", func1)
  33. id2 = plt.add_callback("timer", func2)
  34. printc("Creating Timer Callbacks with IDs:", id1, id2)
  35. # Start two timers, one with a delay of 1s and the other with a delay of 2.3s
  36. ida = plt.timer_callback("start", dt=1000)
  37. idb = plt.timer_callback("start", dt=2300)
  38. printc("Starting timers with IDs :", ida, idb)
  39. # Stop the first timer using its ID
  40. # plt.timer_callback("stop", ida)
  41. plt.show(msh, txt, __doc__, viewup='z')
  42. plt.close()