fyne-gui-demo8选择框组
示例图如下:
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
)
// Layout 界面ui设计--互斥按钮
func Layout() fyne.CanvasObject {
var start *widget.Button
var stop *widget.Button
var label *widget.Label
var checkApple *widget.Check
var checkBanana *widget.Check
// 全局的垂直布局
var fullBox *fyne.Container
// 第一层的水平布局
var Box1 *fyne.Container
// 第二层的水平布局
var Box2 *fyne.Container
var Box3 *fyne.Container
var Box4 *fyne.Container
var checkAppleValue bool
var checkBananaValue = true
// 处理时需按组更新值
checkGroup := widget.NewCheckGroup([]string{"chinese", "english", "french"}, func(strings []string) {
for _, v := range strings {
fmt.Println(v)
}
})
start = widget.NewButton("start", func() {
label.SetText("state:start")
start.Disable()
stop.Enable()
})
stop = widget.NewButton("stop", func() {
label.SetText("state:stop")
start.Enable()
stop.Disable()
})
label = widget.NewLabel("")
// 按钮的初始状态
stop.Disable()
// 标签的初始状态
label.SetText("state:stop")
checkApple = widget.NewCheck("Apple", func(b bool) {
checkAppleValue = b
fmt.Println(checkAppleValue)
})
// 设置含有默认选项的选择框、此方法会失去自定义处理逻辑的实现、如上
checkBanana = widget.NewCheckWithData("Banana", binding.BindBool(&checkBananaValue))
Box1 = container.NewHBox(
label,
)
Box2 = container.NewHBox(
start,
stop,
)
Box3 = container.NewHBox(
checkApple,
checkBanana,
)
Box4 = container.NewHBox(
checkGroup,
)
// 垂直布局
fullBox = container.NewVBox(
Box1,
Box2,
Box3,
Box4,
)
return fullBox
}
func main() {
// 创建程序
thisApp := app.New()
// 创建窗口对象、传入窗口名
w := thisApp.NewWindow("Title")
// 设置窗口内容
w.SetContent(Layout())
// 窗口的显示和运行
w.ShowAndRun()
}