fyne widget小部件1
label标签
Label 小部件是其中最简单的——它向用户呈现文本。不像canvas.Text
它可以处理一些简单的格式(例如\n
)。
package main
import (
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Label Widget")
content := widget.NewLabel("text")
myWindow.SetContent(content)
myWindow.ShowAndRun()
}
button按钮
按钮小部件可以包含文本、图标或两者,构造函数是widget.NewButton()
和widget.NewButtonWithIcon()
。
package main
import (
"log"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Button Widget")
content := widget.NewButton("click me", func() {
log.Println("tapped")
})
myWindow.SetContent(content)
myWindow.ShowAndRun()
}
package main
import (
"fyne.io/fyne/v2/theme"
"log"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Button Widget")
content := widget.NewButtonWithIcon("Home", theme.HomeIcon(), func() {
log.Println("tapped home")
})
myWindow.SetContent(content)
myWindow.ShowAndRun()
}
entry入口
条目小部件用于简单文本内容的用户输入。可以使用简单的widget.NewEntry()
构造函数创建条目。
package main
import (
"log"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Entry Widget")
input := widget.NewEntry()
input.SetPlaceHolder("Enter text...")
content := container.NewVBox(input, widget.NewButton("Save", func() {
log.Println("Content was:", input.Text)
}))
myWindow.SetContent(content)
myWindow.ShowAndRun()
}
单选多选
有多种小部件可用于向用户提供选择,其中包括复选框、单选组和选择弹出窗口。
package main
import (
"log"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Choice Widgets")
// 多选框
check := widget.NewCheck("Optional", func(value bool) {
log.Println("Check set to", value)
})
// 单选按钮组
radio := widget.NewRadioGroup([]string{"Option 1", "Option 2"}, func(value string) {
log.Println("Radio set to", value)
})
// 下拉框
combo := widget.NewSelect([]string{"Option 1", "Option 2"}, func(value string) {
log.Println("Select set to", value)
})
myWindow.SetContent(container.NewVBox(check, radio, combo))
myWindow.ShowAndRun()
}