1

需要帮助在运行时更新 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()
}

谢谢

4

1 回答 1

1

NewTreeWithStrings是一个静态辅助函数,因此不会真正按照您想要的方式进行更新。如果你可以使用NewTree,那么你可以指向你的数据结构,它会自动更新。

于 2021-05-08T21:52:27.717 回答