marcomusy / vedo

A python module for scientific analysis of 3D data based on VTK and Numpy
https://vedo.embl.es
MIT License
1.98k stars 257 forks source link

About the button in vedo. #1138

Closed InFistLee closed 2 weeks ago

InFistLee commented 3 weeks ago

Hi marco.

I am a beginner of vedo and I want to implement a 'play/pause' button in vedo. Here is my basic code:

import vedo

def test_handle_timer(event):
    print('timer event triggered.')

plt = vedo.Plotter(axes=1)
timerevt = plt.add_callback('timer', test_handle_timer)
is_playing = False
timer_id = None
button = None

def button_fnc(obj, ename):
    global is_playing, timer_id, button
    if timer_id is not None:
        plt.timer_callback("destroy", timer_id)
    if not is_playing:
        timer_id = plt.timer_callback("create", dt=100)
    button.switch()
    print('button clicked.')
    is_playing = not is_playing

button = plt.add_button(button_fnc, states=["\u23F5 Play  ","\u23F8 Pause"], size=32)

plt.interactive()
plt.close()

What I expect is when I click the button, I will get one 'button clicked' and many following 'time event triggered'. However, what I actually get is two 'button clicked' and only one 'time event triggered': image It seems like the button_fnc has been triggered twice, once when I click the button and once when I release.

I wonder what is the proper way to implement such button.

marcomusy commented 2 weeks ago

Hi i have written a new class called ButtonWidget to address this issue. Check out:

https://github.com/marcomusy/vedo/blob/master/examples/basic/buttons3.py

InFistLee commented 2 weeks ago

Thanks! This new class perfectly solves my problem.