动态更新 fyne 树小部件

需要帮助在运行时更新树小部件,正如这里所建议的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()

}

谢谢


MM们
浏览 203回答 1
1回答

湖上湖

这是一个静态帮助程序函数,因此不会真正按照您想要的方式进行更新。如果可以使用,则可以指向数据结构,它将自动更新。NewTreeWithStringsNewTree
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go