3.3.1. Common

The methods described in this section are applicable to all custom UI controls except Stage.

common_object.set_active(status)
Description:Sets whether to display the current control
参数:status (bool) – The activity of the control, where True indicates to display the current control, and False indicates to hide the current control
返回:None
Example:my_Slider.set_active(Flase)
Example description:
 Set the my_Slider control to hidden
common_object.get_active()
Description:Obtains the display state of the current control
参数:void – None
返回:A boolean value, which indicates the display state of the control
Example:status = my_Slider.get_active()
Example description:
 Obtain the display state of the my_Slider control and assign it to the status variable
common_object.set_name(name)
Description:Set the name of the current control
参数:name (string) – Name of the control
返回:None
Example:my_Dropdown.set_name('my_dropdown')
Example description:
 Set the name of the my_Dropdown control to “my_dropdown”
common_object.get_name()
Description:Obtains the name of the current control
参数:void – None
返回:A string, which indicates the name of the control
Example:name = my_Dropdown.get_name()
Example description:
 Obtain the name of the my_Dropdown control and assign it to the name variable
common_object.set_position(x, y)
Description:

Sets the coordinates of the control, where the origin is at the center of the screen

参数:
  • x (int) – The abscissa of the control, whose value is the actual pixel position on the screen. The 0 point is at the horizontal center of the screen, and rightward is the positive direction.
  • y (int) – The ordinate of the control, whose value is the actual pixel position on the screen. The 0 point is at the vertical center of the screen, and upward is the positive direction.
返回:

None

Example:

my_Text.set_position(-200, 500)

Example description:
 

Set the coordinates of the my_Text control to (-200, 500)

common_object.get_position()
Description:Obtains the coordinates of the control
参数:void – None
返回:[x,y], which represents the position of the control
Example:pos = my_Text.get_position()
Example description:
 Obtain the position of the my_Text control and assign it to the pos variable, which is a list
common_object.set_size(w, h)
Description:

Sets the size of the control

参数:
  • w (int) – Width of the control
  • h (int) – Height of the control
返回:

None

Example:

my_Button.set_size(300, 200)

Example description:
 

Set the width of the my_Button control to 300 and the height to 200

common_object.get_size()
Description:Obtains the size of the control
参数:void – None
返回:[w,h], which represents the size of the control
Example:size = my_Button.get_size()
Example description:
 Obtain the size of the my_Button control and assign it to the size variable, which is a list
common_object.set_rotation(degree)
Description:Sets the rotation angle of the control
参数:degree (int) – The rotation angle of the control, whose range is [0, 360]. A positive value indicates clockwise rotation, and a negative value indicates counterclockwise rotation.
返回:None
Example:my_Button.set_rotation(90)
Example description:
 Set the my_Button control to rotate 90 degrees clockwise
common_object.get_rotation()
Description:Obtains the rotation angle of the control
参数:void – None
返回:An int value, which indicates the rotation angle of the control, whose range is [0, 360]. A positive value indicates clockwise rotation, and a negative value indicates counterclockwise rotation.
Example:degree = my_Button.get_rotation()
Example description:
 Obtain the rotation angle of the my_Button control and assign it to the degree variable
common_object.set_privot(x, y)
Description:

Sets the anchor coordinates of the control, where the input parameter is a normalized parameter. The origin is at the lower-left corner of the control. The anchor point of the control defaults to the center of the control, that is (0.5, 0.5). The position and rotation of the control are controlled by the anchor point.

参数:
  • x (int) – The x-coordinate of the anchor point, whose range is [0, 1]. Rightward is the positive direction.
  • y (int) – The y-coordinate of the anchor point, whose range is [0, 1]. Upward is the positive direction.
返回:

None

Example:

my_Button.set_privot(0, 1)

Example description:
 

Set the anchor point of the control to the upper-left corner of the control

common_object.get_privot()
Description:Obtains the anchor coordinates of the control
参数:void – None
返回:[x,y], which represents the anchor coordinates of the control
Example:privot = my_Button.get_privot()
Example description:
 Obtain the anchor coordinates of the control and assign them to the privot variable, which is a list
common_object.set_order(order)
Description:Sets the display priority of the control. If multiple controls overlap with each other, the control with higher priority is in the upper layer. The greater the display priority value, the higher the priority.
参数:order (int) – The specified priority of the control. If multiple controls overlap with each other, the control with higher priority is displayed first.
返回:None
Example:my_Button.set_order(8)
Example description:
 Set the display priority of the control to 8. When multiple controls overlap, the controls with priority values lower than this priority value are covered.
common_object.get_order()
Description:Obtains the display priority value of the control
参数:void – None
返回:An int value, which indicates the display priority value of the control
Example:order = my_Button.get_order()
Example description:
 Obtain the display priority value of the my_Button control and assign it to the order variable
common_object.callback_register(event, callback)
Description:

The callback function triggered by the registered control event. When the control detects this event, the registered callback function is executed.

参数:
  • event (string) –

    Specify the trigger event of the callback function

    The events that can be registered for each control are described as follows:

    • The Button control:
      • on_click: Trigger this event when the button is released in the process of pressing and releasing the button once
      • on_press_down: Trigger this event when the button is pressed
      • on_press_up: Trigger this event when the button is released
    • The Toggle control:
      • Trigger this event when the value of on_value_changed changes. The args parameter in the callback function is a boolean value, which is the updated value of the Toggle control.
    • The Dropdown control:
      • Trigger this event when the value of on_value_changed changes. The args parameter in the callback function is an int value, which is the selected index entry after the value of the Toggle control changes.
    • The Text control:
      • No trigger events are available.
    • The InputField control:
      • Trigger this event when the value of on_value_changed changes. The args parameter in the callback function is a string, which is the updated value of the InputField control.
  • callback (function) – The callback function that needs to be registered. The unified signature of callback functions is def callback(widget,*args,**kw):, where widget is the reference to the control that triggered the event, and args and kw are parameters.
返回:

None

Example 1:
1
2
3
4
5
6
# When the my_Button control is clicked, the information will be output to the console, and the robot will shoot once.

def button_callback(widget,*args,**kw):
    print('the button is clicked and the button's name is '+ widget.get_name())
    gun_ctrl.fire_once()
my_Button.callback_register('on_click',button_callback)
example 2:
1
2
3
4
5
6
7
# When the my_Toggle control is clicked, the value of the control changes, the information is output to the console, and the robot will play a sound.

def toggle_callback(widget,*args,**kw):
    print("the toggle's value is changed and the toggle's name is "+ widget.get_name())
    print("the toggle's value now is "+ str(args))
    media_ctrl.play_sound(rm_define.media_sound_recognize_success)
my_Toggle.callback_register('on_value_changed',toggle_callback)
example 3:
1
2
3
4
5
6
7
# When you click the my_Dropdown control to change its selected value, the value changes, the information is output to the console, and the robot will play a sound.

def dropdown_callback(widget,*args,**kw):
    print("the dropdown's value is changed and the dropdown's name is "+ widget.get_name())
    print("the dropdown's value now is "+ str(args))
    media_ctrl.play_sound(rm_define.media_sound_solmization_1A)
my_Dropdown.callback_register('on_value_changed',dropdown_callback)
example 4:
1
2
3
4
5
6
# When you click the my_InputField control to change its selected value, the value changes, and the information is output to the console.

def input_field_callback(widget,*args,**kw):
    print("the input_field's value is changed and the input_field's name is "+ widget.get_name())
    print("the input_field's value now is "+ str(args))
my_InputField.callback_register('on_value_changed',input_field_callback)