Go入门笔记46 Go Walk ListBox使用


Walk是Go的一个开发窗口程序的库,下面以ListBox使用方法做说明
原始代码太长,不利于理解,精简一下

// Copyright 2012 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 (
	"log"

	"github.com/lxn/walk"

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

func main() {
	mw := &MyMainWindow{model: NewEnvModel()}

	if _, err := (MainWindow{
		AssignTo: &mw.MainWindow,
		Title:    "Walk ListBox Example",
		MinSize:  Size{240, 320},
		Size:     Size{300, 400},
		Layout:   VBox{MarginsZero: true},
		Children: []Widget{
			ListBox{
				AssignTo: &mw.lb,
				Model:    mw.model,
			},
		},
	}.Run()); err != nil {
		log.Fatal(err)
	}
}

type MyMainWindow struct {
	*walk.MainWindow
	model *EnvModel
	lb    *walk.ListBox
}

type EnvItem struct {
	name  string
	value string
}

type EnvModel struct {
	walk.ListModelBase
	items []EnvItem
}

func NewEnvModel() *EnvModel {
	m := &EnvModel{items: make([]EnvItem, 3)}

	m.items[0] = EnvItem{"name", "value"}
	m.items[1] = EnvItem{"name", "value"}
	m.items[2] = EnvItem{"name", "value"}
	return m
}

func (m *EnvModel) ItemCount() int {
	return len(m.items)
}

func (m *EnvModel) Value(index int) interface{} {
	return m.items[index].name
}

EnvMode定义

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

    
    
        
            
        
    
    
        
            true
        
    


rsrc -manifest test.manifest -o rsrc.syso
go mod init test
go mod tidy
go build
运行