需要帮助在运行时更新 Tree 小部件,如此处所建议的fyne 问题似乎在小部件上使用 Refresh() 函数它将同步存储在list
地图中的数据更改,这是代码:
package main
//______________________________________________________________________________
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/layout"
"fyne.io/fyne/v2/widget"
dbg "github.com/fatih/color"
)
//______________________________________________________________________________
var content *fyne.Container
var tree *widget.Tree
var list map[string][]string
//______________________________________________________________________________
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("List")
makeTree()
top := widget.NewLabel("Items' list::")
bottom := widget.NewButton("Update list", updateList)
content = container.New(layout.NewBorderLayout(top, bottom, nil, nil),
top,
tree,
bottom,
)
myWindow.SetContent(content)
myWindow.Resize(fyne.NewSize(480, 600))
myWindow.ShowAndRun()
}
//______________________________________________________________________________
func makeTree() {
list = map[string][]string{
"": {"A"},
"A": {"B", "D"},
"B": {"C"},
"C": {"abc"},
"D": {"E"},
"E": {"F", "G"},
}
tree = widget.NewTreeWithStrings(list)
tree.OnSelected = func(id string) {
dbg.Green("Tree node selected: %s", id)
}
tree.OnUnselected = func(id string) {
dbg.Green("Tree node unselected: %s", id)
}
tree.OpenAllBranches()
}
//______________________________________________________________________________
func updateList() {
dbg.Green("UpdateList")
list = map[string][]string{
"": {"A"},
"A": {"B"},
"B": {"C"},
}
tree.Refresh()
}
谢谢