Buttons

_images/button.png

Buttons are widgets that can be placed either in the title area or the caption area of a canvas. A click on a button invokes a function that can perform actions, including changing the text of the button or disabling it.

mybutton = button( bind=myaction, text='Click me!' )
Parameters:
  • bind (function) – The function to be called when the button is clicked.

  • text (string) – Text to appear on the button

  • pos (attribute of canvas) – Location of button. Default is scene.caption_anchor.

  • color (vector) – Color of button text. Modifiable.

  • background (vector) – Color of button background. Modifiable.

  • disabled (boolean) – If True, button is grayed out and does not respond.

  • delete()mybutton.delete() deletes the button.

A button that changes its own color and the color of a sphere:

ball = sphere(color=color.cyan)

def changecolor(evt):
        if evt.text == 'red':
                ball.color=color.red
                clrbtn.background = color.cyan
                clrbtn.text = 'cyan'
        else:
                ball.color=color.cyan
                clrbtn.text = 'red'
                clrbtn.background = color.red

clrbtn = button( bind=changecolor, text='red', background=color.red )

Button Event Attributes

The argument of the event handler function (‘evt’, in the code above) will have the following attributes (properties of the button at the time it was clicked):

  • evt.text

  • evt.color

  • evt.background

  • evt.disabled

Additionally, any attributes you have created for the widget (for example, name or id), will be available as attributes of evt.

Accessibility Attributes

These attributes support ARIA (Accessible Rich Internet Applications), making widgets usable with screen readers and other assistive technology.

aria_label (string)

A text label announced by screen readers when the widget receives focus. Use when there is no visible text that already identifies the widget:

myslider = slider(bind=setspeed, min=0, max=10, aria_label="Speed")
aria_labelledby (wtext)

A wtext object whose text serves as the accessible label for this widget. Use when a visible wtext already describes the widget:

speedlabel = wtext(text="Speed: ")
myslider = slider(bind=setspeed, min=0, max=10, aria_labelledby=speedlabel)
aria_describedby (wtext)

A wtext object that provides a longer accessible description of the widget, supplementing aria_label or aria_labelledby:

hint = wtext(text="Drag left or right to set playback speed from 0 to 10")
myslider = slider(bind=setspeed, min=0, max=10, aria_label="Speed", aria_describedby=hint)