Note
Click here to download the full example code
User Interfaces¶
This example shows how to use the UI API. We will demonstrate how to create several DIPY UI elements, then use a list box to toggle which element is shown.
First, a bunch of imports.
from fury.data import read_viz_icons, fetch_viz_icons
from fury import ui, window
Shapes¶
Let’s start by drawing some simple shapes. First, a rectangle.
rect = ui.Rectangle2D(size=(200, 200), position=(400, 300), color=(1, 0, 1))
Then we can draw a solid circle, or disk.
disk = ui.Disk2D(outer_radius=50, center=(500, 500), color=(1, 1, 0))
Add an inner radius to make a ring.
ring = ui.Disk2D(outer_radius=50, inner_radius=45, center=(500, 300),
color=(0, 1, 1))
Image¶
Now let’s display an image. First we need to fetch some icons that are included in DIPY.
Out:
Data size is approximately 12KB
Dataset is already in place. If you want to fetch it again please first remove the folder C:\Users\skoudoro\.fury\icons
Now we can create an image container.
img = ui.ImageContainer2D(img_path=read_viz_icons(fname='home3.png'),
position=(450, 350))
Panel with buttons and text¶
Let’s create some buttons and text and put them in a panel. First we’ll make the panel.
panel = ui.Panel2D(size=(300, 150), color=(1, 1, 1), align="right")
panel.center = (500, 400)
Then we’ll make two text labels and place them on the panel. Note that we specifiy the position with integer numbers of pixels.
text = ui.TextBlock2D(text='Click me')
text2 = ui.TextBlock2D(text='Me too')
panel.add_element(text, (50, 100))
panel.add_element(text2, (180, 100))
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.
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))
We can add a callback to each button to perform some action.
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
Cube and sliders¶
Let’s add a cube to the scene and control it with sliders.
def cube_maker(color=(1, 1, 1), size=(0.2, 0.2, 0.2), center=(0, 0, 0)):
cube = window.vtk.vtkCubeSource()
cube.SetXLength(size[0])
cube.SetYLength(size[1])
cube.SetZLength(size[2])
if center is not None:
cube.SetCenter(*center)
cube_mapper = window.vtk.vtkPolyDataMapper()
cube_mapper.SetInputConnection(cube.GetOutputPort())
cube_actor = window.vtk.vtkActor()
cube_actor.SetMapper(cube_mapper)
if color is not None:
cube_actor.GetProperty().SetColor(color)
return cube_actor
cube = cube_maker(color=(0, 0, 1), size=(20, 20, 20), center=(15, 0, 0))
Now we’ll add two sliders: one circular and one linear.
ring_slider = ui.RingSlider2D(center=(740, 400), initial_value=0,
text_template="{angle:5.1f}°")
line_slider = ui.LineSlider2D(center=(500, 250), initial_value=0,
min_value=-10, max_value=10)
We can use a callback to rotate the cube with the ring slider.
def rotate_cube(slider):
angle = slider.value
previous_angle = slider.previous_value
rotation_angle = angle - previous_angle
cube.RotateX(rotation_angle)
ring_slider.on_change = rotate_cube
Similarly, we can translate the cube with the line slider.
def translate_cube(slider):
value = slider.value
cube.SetPosition(value, 0, 0)
line_slider.on_change = translate_cube
Range Slider¶
Finally, we can add a range slider. This element is composed of two sliders. The first slider has two handles which let you set the range of the second.
range_slider = ui.RangeSlider(
line_width=8, handle_side=25, range_slider_center=(550, 450),
value_slider_center=(550, 350), length=250, min_value=0,
max_value=10, font_size=18, range_precision=2, value_precision=4,
shape="square")
Show Manager¶
Now that all the elements have been initialised, we add them to the show manager.
current_size = (800, 800)
show_manager = window.ShowManager(size=current_size, title="DIPY UI Example")
show_manager.scene.add(listbox)
for example in examples:
for element in example:
show_manager.scene.add(element)
show_manager.scene.add(cube)
show_manager.scene.reset_camera()
show_manager.scene.set_camera(position=(0, 0, 200))
show_manager.scene.reset_clipping_range()
show_manager.scene.azimuth(30)
interactive = False
if interactive:
show_manager.start()
window.record(show_manager.scene, size=current_size, out_path="viz_ui.png")
Total running time of the script: ( 0 minutes 0.471 seconds)