Skip to content

Button 按钮 ​

触发一个明确动作,例如保存表单、提交查询、打开对话框。Primary 用于主要操作,Danger 表示具有破坏性的动作。

在线体验 ​

点击预览按钮,观察计数变化;Disabled 按钮不会改变状态。 工具栏支持 Ant Design、Element Plus、shadcn 以及深浅色切换;重置会恢复初始状态。

Button 按钮 · 在线体验真实 Go 组件
在独立页面打开 ↗

基本用法 ​

下面代码放在应用的页面文件中;完整的 main 和窗口初始化见 打开自绘窗口。

go
import (
    "github.com/tangoui/tango/ui"
    "fmt"
)

var clicks int

func page(c *ui.Context) {
    if ui.Button(c, "Click me").Primary().Clicked() {
        clicks++
    }
    ui.Text(c, fmt.Sprintf("Clicks: %d", clicks))
}

常用参数与方法 ​

参数 / 方法说明
Primary()主要操作的强调样式
Danger()危险操作样式,可与 Primary 组合
Disabled(bool)禁用交互
Loading(bool)显示加载状态并阻止重复点击
Small() / Large()设置控件尺寸
Clicked() bool读取当前帧是否发生点击

使用说明 ​

使用 Tab 聚焦后,Enter 或空格可以触发按钮。异步操作开始后可使用 Loading(true) 阻止重复提交。

更多场景 ​

官方 gallery 里这个组件的全部示例(基础、进阶与组合场景),可切换场景、主题和深浅色。

Button 按钮 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

实现与更多示例 ​

全部示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Button 按钮

gallery 演示 Button 的 Render 函数,源码 page_general.go#L15。

加载中…

IconButton 图标按钮

gallery 演示 IconButton 的 Render 函数,源码 page_fill_misc.go#L36。

加载中…

Button 图标位置与加载

gallery 演示 ButtonAlign 的 Render 函数,源码 page_b1a.go#L28。

加载中…

ButtonGroup 按钮组

gallery 演示 ButtonGroup 的 Render 函数,源码 page_button_group.go#L67。

加载中…

Button / 选择控件状态

gallery 演示 ShadButtonStates 的 Render 函数,源码 page_shad2a_entry.go#L91。

加载中…

完整 API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Button ​

go
func Button(c *Context, label string) *ButtonW

Button adds a button.

返回 *ButtonW,可链式调用的方法见 ButtonW 方法。

ui.IconButton ​

go
func IconButton(c *Context, ic *icon.Icon, label string) *IconButtonW

IconButton adds an icon button; label is used for tests and accessibility.

返回 *IconButtonW,可链式调用的方法见 IconButtonW 方法。

ButtonW 方法 ​

ui.ButtonW:ButtonW is a button under construction; chain modifiers then query.

方法说明签名
A11yLabelA11yLabel sets the accessible name read by screen readers.A11yLabel(s string) *ButtonW
AppearanceAppearance sets how the button wears its color (Ant v6 variant, plus Element's plain).Appearance(a ButtonAppearance) *ButtonW
AutoInsertSpaceAutoInsertSpace sets whether a label of exactly two Chinese characters is drawn with a space between them ("确定" shows as "确 定"; Ant / Element ConfigProvider button.autoInsertSpace).AutoInsertSpace(on bool) *ButtonW
BlockBlock stretches the button to the container width.Block() *ButtonW
CircleCircle makes an icon-only round button.Circle() *ButtonW
ClickedClicked reports activation (never when disabled or loading).Clicked() bool
ColorColor sets the button color (Ant v6 color): a role, an Ant preset or a hex color ("#13c2c2").Color(col ButtonColor) *ButtonW
DangerDanger() *ButtonW
DashedDashed() *ButtonW
DisabledDisabled disables the button when on.Disabled(on bool) *ButtonW
GhostGhost() *ButtonW
HoveredHovered reports hover.Hovered() bool
IDID returns the widget ID.ID() ID
IconIcon puts an icon before the label.Icon(ic *icon.Icon) *ButtonW
IconPlacementIconPlacement places the icon at the inline "start" of the label (default) or at its "end" (Ant v6 iconPlacement, which replaces iconPosition).IconPlacement(p string) *ButtonW
IconRightIconRight puts the icon after the label (Icon(ic).IconPlacement("end")).IconRight(ic *icon.Icon) *ButtonW
InfoInfo makes a solid info button (Element type="info").Info() *ButtonW
LargeLarge() *ButtonW
LayoutLayout applies layout options.Layout(opts ...layout.Option) *ButtonW
LinkLink() *ButtonW
LoadingLoading shows a spinner and blocks clicks.Loading(on bool) *ButtonW
LoadingDelayLoadingDelay postpones the loading state (Ant loading.delay): after Loading(true) the button stays clickable and shows no spinner until d has passed; a loading that ends sooner never shows.LoadingDelay(d time.Duration) *ButtonW
LoadingIconLoadingIcon replaces the loading spinner with ic, drawn spinning (Ant loading.icon, Element loading-icon).LoadingIcon(ic *icon.Icon) *ButtonW
PlainPlain uses Element's plain style: a light tint of the color with a tinted border, filled with the color under the pointer.Plain() *ButtonW
PrimaryPrimary() *ButtonW
RoundRound() *ButtonW
SecondarySecondary makes a neutral filled button (shadcn "secondary").Secondary() *ButtonW
SizeSize sets the control size.Size(s ControlSize) *ButtonW
SmallSmall() *ButtonW
SuccessSuccess makes a solid success button (Element type="success"); add Plain for the plain style.Success() *ButtonW
TextText() *ButtonW
VariantVariant sets the variant explicitly.Variant(v ButtonVariant) *ButtonW
WarningWarning makes a solid warning button (Element type="warning").Warning() *ButtonW

IconButtonW 方法 ​

ui.IconButtonW:IconButtonW is a borderless square icon button (close buttons, toolbar toggles, calendar navigation).

方法说明签名
A11yLabelA11yLabel replaces the accessible name (by default the label argument).A11yLabel(s string) *IconButtonW
ClickedClicked reports activation.Clicked() bool
ColorColor fixes the icon color.Color(col theme.Color) *IconButtonW
HoveredHovered reports hover.Hovered() bool
NoHoverBgNoHoverBg disables the hover background.NoHoverBg() *IconButtonW
SizeSize sets the square box size and the icon size.Size(box, iconSize float32) *IconButtonW

Icon 参数类型 ​

icon.Icon:Icon is a vector (or bitmap, see FromImage) icon. Declare custom icons with a composite literal, New or FromImage:

var Branch = &icon.Icon{Name: "app:branch", Box: [4]float32{0, 0, 1024, 1024},
	Parts: []icon.Part{{D: "M..."}}}

The exported fields must not change once the icon is first drawn.

字段说明类型
Namestring
BoxBox is the view box (min x, min y, width, height) Parts are drawn in, like an SVG viewBox.[4]float32
WidthWidth is the stroke width of stroked Parts in Box units.float32
PartsParts are painted in order.[]Part
DirDir marks a directional icon (an arrow, a chevron, text lines that start at the left): it mirrors horizontally in right-to-left layouts.bool

Icon 的方法:

方法说明签名
DirectionalDirectional marks ic as directional, so it mirrors horizontally when drawn for a right-to-left layout (see DrawOpts.RTL), and returns ic.Directional() *Icon
IsDirectionalIsDirectional reports whether ic mirrors in right-to-left layouts.IsDirectional() bool
IsImageIsImage reports whether ic is a bitmap icon made by FromImage.IsImage() bool

以 MIT 许可发布