Inheritance #

Table of contents

Button #

is_instantiable, Node, core, not_builtin_classes

A themed button that can contain text and an icon.

Button is the standard themed button. It can contain text and an icon, and it will display them according to the current Theme.

Example: Create a button and connect a method that will be called when the button is pressed:

GDScript

func _ready():
    var button = Button.new()
    button.text = "Click me"
    button.pressed.connect(_button_pressed)
    add_child(button)

func _button_pressed():
    print("Hello world!")

C#

public override void _Ready()
{
    var button = new Button();
    button.Text = "Click me";
    button.Pressed += ButtonPressed;
    AddChild(button);
}

private void ButtonPressed()
{
    GD.Print("Hello world!");
}

See also BaseButton which contains common properties and methods associated with this node.

Note: Buttons do not detect touch input and therefore don't support multitouch, since mouse emulation can only press one button at a given time. Use TouchScreenButton for buttons that trigger gameplay movement or actions.

Members #

var alignment: int = 1#

Text alignment policy for the button's text, use one of the HorizontalAlignment constants.

var autowrap_mode = AUTOWRAP_OFF#

If set to something other than TextServer.AUTOWRAP_OFF, the text gets wrapped inside the node's bounding rectangle.

var clip_text: bool = false#

If true, text that is too large to fit the button is clipped horizontally. If false, the button will always be wide enough to hold the text. The text is not vertically clipped, and the button's height is not affected by this property.

var expand_icon: bool = false#

When enabled, the button's icon will expand/shrink to fit the button's size while keeping its aspect. See also icon_max_width.

var flat: bool = false#

Flat buttons don't display decoration.

var icon: Texture2D#

Button's icon, if text is present the icon will be placed before the text.

To edit margin and spacing of the icon, use h_separation theme property and content_margin_* properties of the used StyleBoxes.

var icon_alignment: int = 0#

Specifies if the icon should be aligned horizontally to the left, right, or center of a button. Uses the same HorizontalAlignment constants as the text alignment. If centered horizontally and vertically, text will draw on top of the icon.

var language: String = ""#

Language code used for line-breaking and text shaping algorithms, if left empty current locale is used instead.

var text: String = ""#

The button's text that will be displayed inside the button's area.

var text_direction = TEXT_DIRECTION_AUTO#

Base text writing direction.

var text_overrun_behavior = OVERRUN_NO_TRIMMING#

Sets the clipping behavior when the text exceeds the node's bounding rectangle. See TextServer.OverrunBehavior for a description of all modes.

var vertical_icon_alignment: int = 1#

Specifies if the icon should be aligned vertically to the top, bottom, or center of a button. Uses the same VerticalAlignment constants as the text alignment. If centered horizontally and vertically, text will draw on top of the icon.

Methods #

Annotations #

Constants #

Constructors #

Enums #

Operators #

Signals #

Theme Items #

self["theme_override_colors/font_color"] = Color(0.875, 0.875, 0.875, 1) as Color#

Default text Color of the Button.

self["theme_override_colors/font_disabled_color"] = Color(0.875, 0.875, 0.875, 0.5) as Color#

Text Color used when the Button is disabled.

self["theme_override_colors/font_focus_color"] = Color(0.95, 0.95, 0.95, 1) as Color#

Text Color used when the Button is focused. Only replaces the normal text color of the button. Disabled, hovered, and pressed states take precedence over this color.

self["theme_override_colors/font_hover_color"] = Color(0.95, 0.95, 0.95, 1) as Color#

Text Color used when the Button is being hovered.

self["theme_override_colors/font_hover_pressed_color"] = Color(1, 1, 1, 1) as Color#

Text Color used when the Button is being hovered and pressed.

self["theme_override_colors/font_outline_color"] = Color(0, 0, 0, 1) as Color#

The tint of text outline of the Button.

self["theme_override_colors/font_pressed_color"] = Color(1, 1, 1, 1) as Color#

Text Color used when the Button is being pressed.

self["theme_override_colors/icon_disabled_color"] = Color(1, 1, 1, 0.4) as Color#

Icon modulate Color used when the Button is disabled.

self["theme_override_colors/icon_focus_color"] = Color(1, 1, 1, 1) as Color#

Icon modulate Color used when the Button is focused. Only replaces the normal modulate color of the button. Disabled, hovered, and pressed states take precedence over this color.

self["theme_override_colors/icon_hover_color"] = Color(1, 1, 1, 1) as Color#

Icon modulate Color used when the Button is being hovered.

self["theme_override_colors/icon_hover_pressed_color"] = Color(1, 1, 1, 1) as Color#

Icon modulate Color used when the Button is being hovered and pressed.

self["theme_override_colors/icon_normal_color"] = Color(1, 1, 1, 1) as Color#

Default icon modulate Color of the Button.

self["theme_override_colors/icon_pressed_color"] = Color(1, 1, 1, 1) as Color#

Icon modulate Color used when the Button is being pressed.

self["theme_override_constants/align_to_largest_stylebox"] = 0 as int#

This constant acts as a boolean. If true, the minimum size of the button and text/icon alignment is always based on the largest stylebox margins, otherwise it's based on the current button state stylebox margins.

self["theme_override_constants/h_separation"] = 4 as int#

The horizontal space between Button's icon and text. Negative values will be treated as 0 when used.

self["theme_override_constants/icon_max_width"] = 0 as int#

The maximum allowed width of the Button's icon. This limit is applied on top of the default size of the icon, or its expanded size if expand_icon is true. The height is adjusted according to the icon's ratio. If the button has additional icons (e.g. CheckBox), they will also be limited.

self["theme_override_constants/line_spacing"] = 0 as int#

Additional vertical spacing between lines (in pixels), spacing is added to line descent. This value can be negative.

self["theme_override_constants/outline_size"] = 0 as int#

The size of the text outline.

Note: If using a font with FontFile.multichannel_signed_distance_field enabled, its FontFile.msdf_pixel_range must be set to at least twice the value of outline_size for outline rendering to look correct. Otherwise, the outline may appear to be cut off earlier than intended.

self["theme_override_fonts/font"] = font as Font#

Font of the Button's text.

self["theme_override_font_sizes/font_size"] = font_size as int#

Font size of the Button's text.

self["theme_override_icons/icon"] = icon as Texture2D#

Default icon for the Button. Appears only if icon is not assigned.

self["theme_override_styles/disabled"] = style as StyleBox#

StyleBox used when the Button is disabled.

self["theme_override_styles/disabled_mirrored"] = style as StyleBox#

StyleBox used when the Button is disabled (for right-to-left layouts).

self["theme_override_styles/focus"] = style as StyleBox#

StyleBox used when the Button is focused. The focus StyleBox is displayed over the base StyleBox, so a partially transparent StyleBox should be used to ensure the base StyleBox remains visible. A StyleBox that represents an outline or an underline works well for this purpose. To disable the focus visual effect, assign a StyleBoxEmpty resource. Note that disabling the focus visual effect will harm keyboard/controller navigation usability, so this is not recommended for accessibility reasons.

self["theme_override_styles/hover"] = style as StyleBox#

StyleBox used when the Button is being hovered.

self["theme_override_styles/hover_mirrored"] = style as StyleBox#

StyleBox used when the Button is being hovered (for right-to-left layouts).

self["theme_override_styles/hover_pressed"] = style as StyleBox#

StyleBox used when the Button is being pressed and hovered at the same time.

self["theme_override_styles/hover_pressed_mirrored"] = style as StyleBox#

StyleBox used when the Button is being pressed and hovered at the same time (for right-to-left layouts).

self["theme_override_styles/normal"] = style as StyleBox#

Default StyleBox for the Button.

self["theme_override_styles/normal_mirrored"] = style as StyleBox#

Default StyleBox for the Button (for right-to-left layouts).

self["theme_override_styles/pressed"] = style as StyleBox#

StyleBox used when the Button is being pressed.

self["theme_override_styles/pressed_mirrored"] = style as StyleBox#

StyleBox used when the Button is being pressed (for right-to-left layouts).

Tutorials #