登录
首页 >  Golang >  Go问答

如何从 golang fyne 容器中删除对象

来源:stackoverflow

时间:2024-04-01 21:27:19 129浏览 收藏

知识点掌握了,还需要不断练习才能熟练运用。下面golang学习网给大家带来一个Golang开发实战,手把手教大家学习《如何从 golang fyne 容器中删除对象》,在实现功能的过程中也带大家重新温习相关知识点,温故而知新,回头看看说不定又有不一样的感悟!

问题内容

我正在开发 gui 应用程序,需要动态添加和删除 gui 元素,我想知道是否有办法从 golang fyne 容器中删除元素。在下面的示例代码中,我创建了容器并动态添加元素,现在我希望能够删除这些元素而不是隐藏它们。 (我尝试的一个“解决方案”是创建新容器,填充它并重置窗口内容,但它闪烁,我不喜欢它)。

package main

import (
    "fyne.io/fyne"
    "fyne.io/fyne/app"
    "fyne.io/fyne/canvas"
    "fyne.io/fyne/layout"
    "fyne.io/fyne/widget"
    "image/color"
    "strconv"
    "time"
)

type Input struct {
    Id       string
    ThumbUrl string
}

func (input Input) GetContainer() *fyne.Container {
    thumbImg := canvas.NewText("There", color.White)
    group := widget.NewGroup(input.Id, thumbImg)
    ret := fyne.NewContainerWithLayout(layout.NewVBoxLayout(), group)
    return ret
}

type Segment struct {
    Id           string
    Inputs       []Input
    InputsColumn *fyne.Container
}

func (seg *Segment) GetSegment() fyne.CanvasObject {
    first := true
    var inputsColumn *fyne.Container
    for _, in := range seg.Inputs {
        if first {
            inputsColumn = fyne.NewContainerWithLayout(layout.NewGridLayout(1), in.GetContainer())
            first = false
        } else {
            inputsColumn.AddObject(in.GetContainer())
        }
    }
    seg.InputsColumn = inputsColumn

    ret := fyne.NewContainerWithLayout(layout.NewHBoxLayout(), seg.InputsColumn)
    return ret
}

func (seg *Segment) AddInput(input Input) {
    seg.InputsColumn.AddObject(input.GetContainer())
}

var (
    segment1 = Segment{
        Id: "VSEG_1",
        Inputs: []Input{
            {
                Id:       "VIN_HDMI1",
                ThumbUrl: "thumbIN.png",
            },
            {
                Id:       "VIN_SDI1",
                ThumbUrl: "thumbIN.png",
            },
            {
                Id:       "VIN_SDVOE1",
                ThumbUrl: "thumbIN.png",
            },
        },
    }
)

func main() {
    myApp := app.New()
    myWindow := myApp.NewWindow("Container")
    segmentLayout := segment1.GetSegment()
    lay := fyne.NewContainerWithLayout(layout.NewHBoxLayout(), segmentLayout)

    myWindow.SetContent(lay)
    go changeContent(myWindow)
    myWindow.ShowAndRun()
}

func changeContent(win fyne.Window) {
    for i := 0; i < 3; i++ {
        time.Sleep(time.Second * 2)
        newInput := Input{
            Id:       "Added_ID_" + strconv.Itoa(i),
            ThumbUrl: "thumbIN.png",
        }
        segment1.AddInput(newInput)
        segment1.InputsColumn.Refresh()

    }
    for i := 0; i < 3; i++ {
        time.Sleep(time.Second * 2)
        // Remove the objects instead of hiding
        segment1.InputsColumn.Objects[i].Hide()
        segment1.InputsColumn.Refresh()
    }
    segment1.InputsColumn.Refresh()

}

解决方案


使用 fyne.container,您可以直接更改 objects 字段。例如,您可以使用以下命令删除添加的第一个项目:

l1 := widget.NewLabel("first")
l2 := widget.NewLabel("second")
c := fyne.NewContainer(l1, l2)
log.Println(len(c.Objects)) // 2

c.Objects = c.Objects[:1]
c.Refresh() // ensures UI reflects the change
log.Println(len(c.Objects)) // 1

今天关于《如何从 golang fyne 容器中删除对象》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>