Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Icons instead of Button text? #5817

Closed
3 tasks done
decamargo10 opened this issue Dec 28, 2022 · 2 comments
Closed
3 tasks done

Icons instead of Button text? #5817

decamargo10 opened this issue Dec 28, 2022 · 2 comments
Labels

Comments

@decamargo10
Copy link

Checklist

My Question

Hello,
is there any way to add icons to the Button Widget? The documentation does not mention anything in this direction but it would help me to improve the user experience a lot.
If this is not a feature yet: are there any workarounds that some of you used (e.g. using mouse events and an ImageWidget)?

Thank you in advance!

@cansik
Copy link
Contributor

cansik commented Mar 15, 2023

Since @MartinEekGerhardsen has implemented mouse and key events for image widgets #4939 🥳, it is possible to create ImageButtons quite simply (in 0.17.0). Here a basic example on how to create one:

from typing import Callable, Optional

from open3d.cpu.pybind.visualization.gui import Widget, MouseEvent
from open3d.visualization import gui


class ImageButton(gui.ImageWidget):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.set_on_mouse(self._handle_mouse_event)
        self._event_handler: Optional[Callable[[], None]] = None

    def set_on_clicked(self, event: Callable[[], None]):
        self._event_handler = event

    def _handle_mouse_event(self, e: MouseEvent) -> Widget.EventCallbackResult:
        if e.type == MouseEvent.BUTTON_DOWN:
            if self._event_handler is not None:
                self._event_handler()

        return Widget.EventCallbackResult.CONSUMED

And use it like this:

button = ImageButton("your-icon.png")

def on_click():
    print("clicked")

button.set_on_clicked(on_click)
return button

Of course there could be a more advanced version with multiple images (switch image on hover and so on), but at least it is possible now.

@decamargo10
Copy link
Author

That's great, I'll check it out right away. Thank you very much for your reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants