go-walk gui 编写工具类

package main

import (
	"fmt"
	"github.com/lxn/walk"
	. "github.com/lxn/walk/declarative"
	"strconv"
	"strings"
)

func main() {
	mw := &MyMainWindow{}
	if err := (MainWindow{
		Title: "工具",
		// 指定窗口的大小
		MinSize: Size{},
		AssignTo: &mw.MainWindow,
		Layout: VBox{
			MarginsZero: true,
		},
		Children: []Widget{
			HSplitter{
				MaxSize: Size{0, 50},
				Children: []Widget{
					Label{Text: "  类型: "},
					LineEdit{
						AssignTo: &mw.outtitle,
						ReadOnly: true,
						MaxSize: Size{Height: 4,Width: 1},
					},
					Label{Text: "      功能:   "},
					RadioButtonGroup{
						Buttons: []RadioButton{
							RadioButton{
								Name:     "all",
								Text:     "以 ,号隔开",
								Value:    "1",
								AssignTo: &mw.type_1,
							},
							RadioButton{
								Name:     "all",
								Text:     "转化成用“,”连接",
								Value:    "2",
								AssignTo: &mw.type_2,
							},
							RadioButton{
								Name:     "all",
								Text:     "转化成用空格连接",
								Value:    "3",
								AssignTo: &mw.type_3,
							},
						},
					},
					Label{Text: "            数量: 0",AssignTo: &mw.numLabel},
				},
			},
			VSplitter{
							Children: []Widget{
								TextEdit{AssignTo: &mw.inTE,HScroll:true,VScroll:true},
								TextEdit{AssignTo: &mw.outTE, ReadOnly: true,HScroll:true,VScroll:true},
								PushButton{
									AssignTo: &mw.scream,
									Text: "运行",

								},
							},
						},
		},
	}.Create()); err != nil {
		return
	}

	mw.type_1.Clicked().Attach(func() {
		go func() {
			mw.SetType(mw.type_1.Value(),",号隔开")
		}()
	})
	mw.type_2.Clicked().Attach(func() {
		go func() {
			mw.SetType(mw.type_2.Value(),"转化成用’,‘连接")
		}()
	})
	mw.type_3.Clicked().Attach(func() {
		go func() {
			mw.SetType(mw.type_3.Value(),"转化成用空格连接")
		}()
	})

	mw.scream.Clicked().Attach(func() {
		if mw.type_id == 0 {
			walk.MsgBox(mw, "提示", "请选择功能选项", walk.MsgBoxIconWarning)
			return
		}

		switch mw.type_id {
		case 1:
			go func() {
				mw.run_type_1_scream()
			}()

			break
		case 2:
			go func() {
				mw.run_type_2_scream()
			}()
			break
		case 3:
			go func() {
				mw.run_type_3_scream()
			}()
			break
		}
	})

	mw.Run()


}

type MyMainWindow struct {
	*walk.MainWindow
	lb          *walk.ListBox
	te          *walk.TextEdit
	wv          *walk.WebView
	type_1         *walk.RadioButton
	type_2         *walk.RadioButton
	type_3         *walk.RadioButton
	scream      *walk.PushButton
	curtitle    string
	cururl      string
	outtitle    *walk.LineEdit
	out_num    *walk.LineEdit
	inTE        *walk.TextEdit
	outTE       *walk.TextEdit
	type_id   int
	typeLabel    *walk.Label
	numLabel    *walk.Label
}


func (this *MyMainWindow) SetType(type_id interface{},str string)  {
	fmt.Println(type_id)
	this.type_id,_ = strconv.Atoi(type_id.(string))
	this.outtitle.SetText(strings.ToUpper(str))
}

/**
,号隔开
 */
func (this *MyMainWindow) run_type_1_scream()  {
	str := this.inTE.Text()
	result := removeDuplicateElement(strings.Split(str,"\r\n"))
	this.count_sum(len(result))
	this.outTE.SetText(strings.ToUpper(strings.Join(result,",")))
}
/**
转化成用’,‘连接
*/
func (this *MyMainWindow) run_type_2_scream()  {
	str := this.inTE.Text()
	result := removeDuplicateElement(strings.Split(str,"\r\n"))
	result_str := []string{}
	for _,v := range  result {
		result_str =append( result_str,`"` + v +`"`)
	}
	this.count_sum(len(result_str))
	this.outTE.SetText(strings.ToUpper(strings.Join(result_str,",")))
}

/**
转化成用空格连接
 */
func (this *MyMainWindow) run_type_3_scream()  {
	str := this.inTE.Text()
	result := removeDuplicateElement(strings.Split(str,"\r\n"))
	this.count_sum(len(result))
	this.outTE.SetText(strings.ToUpper(strings.Join(result," ")))
}


func (this *MyMainWindow) count_sum(num int) {
	title := "            数量: " + strconv.Itoa(num)
	this.numLabel.SetText(title)
}


//去重
func removeDuplicateElement(languages []string) []string {
	result := make([]string, 0, len(languages))
	temp := map[string]struct{}{}
	for _, item := range languages {
		if _, ok := temp[item]; !ok {
			temp[item] = struct{}{}
			result = append(result, item)
		}
	}
	return result
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值