Up to date

This page is up to date for Godot 4.2. If you still find outdated information, please open an issue.

Button

Inherits: BaseButton < Control < CanvasItem < Node < Object

Inherited By: CheckBox, CheckButton, ColorPickerButton, MenuButton, OptionButton

A themed button that can contain text and an icon.

Description

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

Example of creating a button and assigning an action when pressed by code:

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

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

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

Note: Buttons do not interpret 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.

Tutorials

Properties

HorizontalAlignment

alignment

1

bool

clip_text

false

bool

expand_icon

false

bool

flat

false

Texture2D

icon

HorizontalAlignment

icon_alignment

0

String

language

""

String

text

""

TextDirection

text_direction

0

OverrunBehavior

text_overrun_behavior

0

VerticalAlignment

vertical_icon_alignment

1

Theme Properties

Color

font_color

Color(0.875, 0.875, 0.875, 1)

Color

font_disabled_color

Color(0.875, 0.875, 0.875, 0.5)

Color

font_focus_color

Color(0.95, 0.95, 0.95, 1)

Color

font_hover_color

Color(0.95, 0.95, 0.95, 1)

Color

font_hover_pressed_color

Color(1, 1, 1, 1)

Color

font_outline_color

Color(1, 1, 1, 1)

Color

font_pressed_color

Color(1, 1, 1, 1)

Color

icon_disabled_color

Color(1, 1, 1, 0.4)

Color

icon_focus_color

Color(1, 1, 1, 1)

Color

icon_hover_color

Color(1, 1, 1, 1)

Color

icon_hover_pressed_color

Color(1, 1, 1, 1)

Color

icon_normal_color

Color(1, 1, 1, 1)

Color

icon_pressed_color

Color(1, 1, 1, 1)

int

h_separation

4

int

icon_max_width

0

int

outline_size

0

Font

font

int

font_size

Texture2D

icon

StyleBox

disabled

StyleBox

disabled_mirrored

StyleBox

focus

StyleBox

hover

StyleBox

hover_mirrored

StyleBox

hover_pressed

StyleBox

hover_pressed_mirrored

StyleBox

normal

StyleBox

normal_mirrored

StyleBox

pressed

StyleBox

pressed_mirrored


Property Descriptions

HorizontalAlignment alignment = 1

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


bool clip_text = false

  • void set_clip_text ( bool value )

  • bool get_clip_text ( )

When this property is enabled, text that is too large to fit the button is clipped, when disabled the Button will always be wide enough to hold the text.


bool expand_icon = false

  • void set_expand_icon ( bool value )

  • bool is_expand_icon ( )

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


bool flat = false

  • void set_flat ( bool value )

  • bool is_flat ( )

Flat buttons don't display decoration.


Texture2D icon

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.


HorizontalAlignment icon_alignment = 0

  • void set_icon_alignment (