Go GUI---lxn/walk 自带demo学习---17.radiobutton单选框

本文介绍了如何使用 Walk 库实现一个简单的放射按钮组,通过 DataBinder 实现数据绑定,并展示如何根据用户选择实时更新相关标签的状态。通过Foo结构体的Bar和Baz属性,展示了如何在前后端交互中进行状态同步。
摘要由CSDN通过智能技术生成

radiobutton.go

// Copyright 2013 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"fmt"
)

import (
	. "github.com/lxn/walk/declarative"
)

type Foo struct {
	Bar string
	Baz int
}

func main() {
	foo := &Foo{"b", 0}

	MainWindow{
		Title:   "Walk RadioButton Example",
		MinSize: Size{320, 240},
		Layout:  VBox{},
		DataBinder: DataBinder{
			DataSource: foo,
			AutoSubmit: true,
			OnSubmitted: func() {
				fmt.Println(foo)
			},
		},
		Children: []Widget{
			// RadioButtonGroup is needed for data binding only.
			RadioButtonGroup{
				DataMember: "Bar",
				Buttons: []RadioButton{
					RadioButton{
						Name:  "aRB",
						Text:  "A",
						Value: "a",
					},
					RadioButton{
						Name:  "bRB",
						Text:  "B",
						Value: "b",
					},
					RadioButton{
						Name:  "cRB",
						Text:  "C",
						Value: "c",
					},
				},
			},
			Label{
				Text:    "A",
				Enabled: Bind("aRB.Checked"),
			},
			Label{
				Text:    "B",
				Enabled: Bind("bRB.Checked"),
			},
			Label{
				Text:    "C",
				Enabled: Bind("cRB.Checked"),
			},
			RadioButtonGroup{
				DataMember: "Baz",
				Buttons: []RadioButton{
					RadioButton{
						Name:  "oneRB",
						Text:  "1",
						Value: 1,
					},
					RadioButton{
						Name:  "twoRB",
						Text:  "2",
						Value: 2,
					},
					RadioButton{
						Name:  "threeRB",
						Text:  "3",
						Value: 3,
					},
				},
			},
			Label{
				Text:    "1",
				Enabled: Bind("oneRB.Checked"),
			},
			Label{
				Text:    "2",
				Enabled: Bind("twoRB.Checked"),
			},
			Label{
				Text:    "3",
				Enabled: Bind("threeRB.Checked"),
			},
		},
	}.Run()
}

 

以下是一个基于Go语言的walk库实现的多页窗口示例: ```go package main import ( "github.com/lxn/walk" . "github.com/lxn/walk/declarative" ) func main() { mw := new(MyMainWindow) if err := (MainWindow{ AssignTo: &mw.MainWindow, Title: "Multi-Page Example", MinSize: Size{300, 200}, Layout: VBox{}, Children: []Widget{ HSpacer{}, RadioButtonGroup{ Buttons: []RadioButton{ {Text: "Page 1", Value: 0}, {Text: "Page 2", Value: 1}, {Text: "Page 3", Value: 2}, }, Orientation: Horizontal, OnValueChanged: func(value int) { mw.Stack.SetCurrentIndex(value) }, }, HSpacer{}, Stack{ AssignTo: &mw.Stack, Pages: []Page{ {Title: "Page 1", Layout: VBox{}, Children: []Widget{ Label{Text: "This is Page 1"}, }}, {Title: "Page 2", Layout: VBox{}, Children: []Widget{ Label{Text: "This is Page 2"}, PushButton{Text: "Button 2"}, }}, {Title: "Page 3", Layout: VBox{}, Children: []Widget{ Label{Text: "This is Page 3"}, LineEdit{AssignTo: &mw.LineEdit}, PushButton{Text: "Button 3", OnClicked: func() { walk.MsgBox(mw, "Button 3", mw.LineEdit.Text(), walk.MsgBoxIconInformation) }}, }}, }, }, HSpacer{}, }, }.Create()); err != nil { panic(err) } mw.Run() } type MyMainWindow struct { *walk.MainWindow Stack *walk.Stack LineEdit *walk.LineEdit } ``` 该示例包含一个主窗口,其中有一个单选按钮组用于切换三个不同的页面。每个页面都包含一个不同的小部件,比如标签、按钮和文本框。使用Stack小部件,可以轻松地在这些页面之间进行切换。 注意:为了使该示例正常工作,需要安装walk库。可以使用以下命令在终端中安装walk库: ``` go get github.com/lxn/walk ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值