Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

Button

繼承: BaseButton < Control < CanvasItem < Node < Object

被繼承: CheckBox, CheckButton, ColorPickerButton, MenuButton, OptionButton

按鈕,支援主題,能夠包含文字和圖示。

說明

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:

func _ready():
    var button = Button.new()
    button.text = "Click me"
    button.pressed.connect(_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 support multitouch via touch input, allowing multiple buttons to be pressed at the same time. Otherwise, mouse input is used, limiting interaction to one button press at a time.

教學

屬性

HorizontalAlignment

alignment

1

AutowrapMode

autowrap_mode

0

BitField[LineBreakFlag]

autowrap_trim_flags

128

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

主題屬性

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(0, 0, 0, 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

align_to_largest_stylebox

0

int

h_separation

4

int

icon_max_width

0

int

line_spacing

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


屬性說明

HorizontalAlignment alignment = 1 🔗

Text alignment policy for the button's text.


AutowrapMode autowrap_mode = 0 🔗

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


BitField[LineBreakFlag] autowrap_trim_flags = 128 🔗

Autowrap space trimming flags. See TextServer.BREAK_TRIM_START_EDGE_SPACES and TextServer.BREAK_TRIM_END_EDGE_SPACES for more info.


bool clip_text = false 🔗

  • void set_clip_text(value: bool)

  • bool get_clip_text()

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.


bool expand_icon = false 🔗

  • void set_expand_icon(value: bool)

  • bool is_expand_icon()

啟用後,將在保持按鈕圖示長寬比的前提下對該圖示進行擴充/收縮,從而適應按鈕的大小。另見 icon_max_width


bool flat = false 🔗

  • void set_flat(value: bool)

  • bool is_flat()

平面按鈕不顯示裝飾。


Texture2D icon 🔗

按鈕的圖示,如果文字存在,則圖示將被放置在文字之前。

要編輯圖示的邊距和間距,請使用 h_separation 主題屬性,和所用 StyleBoxcontent_margin_* 屬性。


HorizontalAlignment icon_alignment = 0 🔗

指定圖示在按鈕上水平對齊的方式應該為左對齊、右對齊還是居中對齊。請使用與文字對齊相同的 HorizontalAlignment 常數。如果水平居中並且垂直居中,則文字將被繪製在圖示之上。


String language = "" 🔗

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


String text = "" 🔗

該按鈕的文字,將顯示在按鈕的區域內。


TextDirection text_direction = 0 🔗

基礎文字書寫方向。


OverrunBehavior text_overrun_behavior = 0 🔗

Sets the clipping behavior when the text exceeds the node's bounding rectangle.


VerticalAlignment vertical_icon_alignment = 1 🔗

指定圖示在按鈕上垂直對齊的方式應該為頂端對齊、底部對齊還是居中對齊。請使用與文字對齊相同的 VerticalAlignment 常數。如果水平居中並且垂直居中,則文本將被繪製在圖示之上。


主題屬性說明

Color font_color = Color(0.875, 0.875, 0.875, 1) 🔗

Button 的預設文字 Color


Color font_disabled_color = Color(0.875, 0.875, 0.875, 0.5) 🔗

Button 處於禁用狀態時,使用的文字 Color


Color font_focus_color = Color(0.95, 0.95, 0.95, 1) 🔗

Button 處於聚焦狀態時,使用的文字 Color。只替換該按鈕的正常文字顏色。禁用、懸停、按下狀態優先於這個顏色。


Color font_hover_color = Color(0.95, 0.95, 0.95, 1) 🔗

Button 處於懸停狀態時,使用的文字 Color


Color font_hover_pressed_color = Color(1, 1, 1, 1) 🔗

Button 處於懸停並按下狀態時,使用的文字 Color


Color font_outline_color = Color(0, 0, 0, 1) 🔗

Button 的文字輪廓的色調。


Color font_pressed_color = Color(1, 1, 1, 1) 🔗

Button 處於按下狀態時,使用的文字 Color


Color icon_disabled_color = Color(1, 1, 1, 0.4) 🔗

Button 處於禁用狀態時,使用的圖示調變 Color


Color icon_focus_color = Color(1, 1, 1, 1) 🔗

Button 處於聚焦狀態時,使用的圖示調變 Color。僅替換該按鈕的正常調變顏色。禁用、懸停和按下狀態優先於這個顏色。


Color icon_hover_color = Color(1, 1, 1, 1) 🔗

Button 處於懸停狀態時,使用的圖示調變Color


Color icon_hover_pressed_color = Color(1, 1, 1, 1) 🔗

Button 處於懸停並按下按下狀態時,使用的圖示調變 Color


Color icon_normal_color = Color(1, 1, 1, 1) 🔗

Button 的預設圖示調變 Color


Color icon_pressed_color = Color(1, 1, 1, 1) 🔗

Button 處於按下狀態時,使用的圖示調變 Color


int align_to_largest_stylebox = 0 🔗

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.


int h_separation = 4 🔗

Button 的圖示和文字之間的水平間距。使用時會將負值當作 0


int icon_max_width = 0 🔗

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.


int line_spacing = 0 🔗

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


int outline_size = 0 🔗

文字輪廓的大小。

注意:如果使用啟用了 FontFile.multichannel_signed_distance_field 的字形,其 FontFile.msdf_pixel_range 必須至少設定為 outline_size兩倍,輪廓算繪才能看起來正確。否則,輪廓可能會比預期的更早被切斷。


Font font 🔗

Button 文字的 Font


int font_size 🔗

Button 文字的字形大小。


Texture2D icon 🔗

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


StyleBox disabled 🔗

Button 處於禁用狀態時使用的 StyleBox


StyleBox disabled_mirrored 🔗

OptionButton 處於禁用狀態時使用的 StyleBox(用於從右至左佈局)。


StyleBox focus 🔗

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.


StyleBox hover 🔗

Button 處於懸停狀態時使用的 StyleBox


StyleBox hover_mirrored 🔗

OptionButton 處於懸停狀態時使用的 StyleBox(用於從右至左佈局)。


StyleBox hover_pressed 🔗

Button 處於按下狀態時使用的 StyleBox


StyleBox hover_pressed_mirrored 🔗

OptionButton 處於按下狀態時使用的 StyleBox(用於從右至左佈局)。


StyleBox normal 🔗

Button 的預設 StyleBox


StyleBox normal_mirrored 🔗

OptionButton 的預設 StyleBox(用於從右至左佈局)。


StyleBox pressed 🔗

Button 處於按下狀態時使用的 StyleBox


StyleBox pressed_mirrored 🔗

OptionButton 處於按下狀態時使用的 StyleBox(用於從右至左佈局)。