.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/07_ui/viz_buttons.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_07_ui_viz_buttons.py: =============== Buttons & Text =============== This example shows how to use the UI API. We will demonstrate how to create panel having buttons with callbacks. First, some imports. .. GENERATED FROM PYTHON SOURCE LINES 12-15 .. code-block:: Python from fury import ui, window from fury.data import fetch_viz_icons, read_viz_icons .. GENERATED FROM PYTHON SOURCE LINES 16-17 First we need to fetch some icons that are included in FURY. .. GENERATED FROM PYTHON SOURCE LINES 17-20 .. code-block:: Python fetch_viz_icons() .. rst-class:: sphx-glr-script-out .. code-block:: none Data size is approximately 12KB Dataset is already in place. If you want to fetch it again please first remove the folder /Users/skoudoro/.fury/icons ({'icomoon.tar.gz': ('https://digital.lib.washington.edu/researchworks/bitstream/handle/1773/38478/icomoon.tar.gz', 'BC1FEEA6F58BA3601D6A0B029EB8DFC5F352E21F2A16BA41099A96AA3F5A4735')}, '/Users/skoudoro/.fury/icons') .. GENERATED FROM PYTHON SOURCE LINES 21-23 Let's create some buttons and text and put them in a panel. First we'll make the panel. .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: Python panel = ui.Panel2D(size=(300, 150), color=(1, 1, 1), align='right') panel.center = (500, 400) .. GENERATED FROM PYTHON SOURCE LINES 28-30 Then we'll make two text labels and place them on the panel. Note that we specify the position with integer numbers of pixels. .. GENERATED FROM PYTHON SOURCE LINES 30-36 .. code-block:: Python text = ui.TextBlock2D(text='Click me') text2 = ui.TextBlock2D(text='Me too') panel.add_element(text, (50, 100)) panel.add_element(text2, (180, 100)) .. GENERATED FROM PYTHON SOURCE LINES 37-41 Then we'll create two buttons and add them to the panel. Note that here we specify the positions with floats. In this case, these are percentages of the panel size. .. GENERATED FROM PYTHON SOURCE LINES 41-59 .. code-block:: Python button_example = ui.Button2D( icon_fnames=[('square', read_viz_icons(fname='stop2.png'))] ) icon_files = [] icon_files.append(('down', read_viz_icons(fname='circle-down.png'))) icon_files.append(('left', read_viz_icons(fname='circle-left.png'))) icon_files.append(('up', read_viz_icons(fname='circle-up.png'))) icon_files.append(('right', read_viz_icons(fname='circle-right.png'))) second_button_example = ui.Button2D(icon_fnames=icon_files) panel.add_element(button_example, (0.25, 0.33)) panel.add_element(second_button_example, (0.66, 0.33)) .. GENERATED FROM PYTHON SOURCE LINES 60-61 We can add a callback to each button to perform some action. .. GENERATED FROM PYTHON SOURCE LINES 61-76 .. code-block:: Python def change_text_callback(i_ren, _obj, _button): text.message = 'Clicked!' i_ren.force_render() def change_icon_callback(i_ren, _obj, _button): _button.next_icon() i_ren.force_render() button_example.on_left_mouse_button_clicked = change_text_callback second_button_example.on_left_mouse_button_pressed = change_icon_callback .. GENERATED FROM PYTHON SOURCE LINES 77-79 Now that all the elements have been initialised, we add them to the show manager. .. GENERATED FROM PYTHON SOURCE LINES 79-91 .. code-block:: Python current_size = (800, 800) show_manager = window.ShowManager(size=current_size, title='FURY Button Example') show_manager.scene.add(panel) interactive = False if interactive: show_manager.start() window.record(show_manager.scene, size=current_size, out_path='viz_button.png') .. image-sg:: /auto_examples/07_ui/images/sphx_glr_viz_buttons_001.png :alt: viz buttons :srcset: /auto_examples/07_ui/images/sphx_glr_viz_buttons_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.071 seconds) .. _sphx_glr_download_auto_examples_07_ui_viz_buttons.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: viz_buttons.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: viz_buttons.py ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_