how to loop over fyne.CanvasObject in fyne in golang

huangapple go评论75阅读模式
英文:

how to loop over fyne.CanvasObject in fyne in golang

问题

问题是,我在NewVScroll中有一个NewVScroll,在NewVScroll中有一个NewVBoxLayout,在NewVBoxLayout中有一个NewButton问题是,当我按下NewButton时,它应该循环遍历NewVScroll,然后循环遍历NewVBoxLayout以找到NewButton。但是我不知道在fyne中如何做到这一点,因为我无法循环遍历**var v1 fyne.CanvasObject**。

错误是:cannot range over v1 (variable of type fyne.CanvasObject)

这是代码:

package main

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

var cEntries *fyne.Container

func FindObject(object fyne.CanvasObject, objects []fyne.CanvasObject) (int, bool) {
	for k1, v1 := range objects {
		for _, v2 := range v1 {// 这里有问题。我无法循环遍历fyne.CanvasObject
			if v2 == object {
				return k1, true
			}
		}
	}
	return 0, false
}

func w1() *fyne.Container {
	wb := widget.NewButton("", nil)

	wb.OnTapped = func() {
		index, isin := FindObject(wb, cEntries.Objects)
		fmt.Println(index, isin)
	}

	return container.New(layout.NewVBoxLayout(), wb)
}

func main() {
	a := app.New()
	w := a.NewWindow("")

	wbAdd := widget.NewButton("+", nil)
	cEntries = container.New(layout.NewVBoxLayout(), wbAdd, w1())

	wbAdd.OnTapped = func() {
		cEntries.Add(w1())
	}

	wsEntries := container.NewVScroll(cEntries)

	w.SetContent(wsEntries)
	w.ShowAndRun()
}
英文:

the question is. i have NewVScroll in the NewVScroll i have NewVBoxLayout in NewVBoxLayout i have NewButton . the problem is when i press the NewButton it should loop over NewVScroll and then loop over NewVBoxLayout to find NewButton . but i don't know how to do it in fyne because i can't loop over var v1 fyne.CanvasObject

the error is : cannot range over v1 (variable of type fyne.CanvasObject)

this is the code

package main

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

var cEntries *fyne.Container

func FindObject(object fyne.CanvasObject, objects []fyne.CanvasObject) (int, bool) {
	for k1, v1 := range objects {
		for _, v2 := range v1 {// the problem is here. i can't loop over fyne.CanvasObject
			if v2 == object {
				return k1, true
			}
		}
	}
	return 0, false
}

func w1() *fyne.Container {
	wb := widget.NewButton("", nil)

	wb.OnTapped = func() {
		index, isin := FindObject(wb, cEntries.Objects)
		fmt.Println(index, isin)
	}

	return container.New(layout.NewVBoxLayout(), wb)
}

func main() {
	a := app.New()
	w := a.NewWindow("")

	wbAdd := widget.NewButton("+", nil)
	cEntries = container.New(layout.NewVBoxLayout(), wbAdd, w1())

	wbAdd.OnTapped = func() {
		cEntries.Add(w1())
	}

	wsEntries := container.NewVScroll(cEntries)

	w.SetContent(wsEntries)
	w.ShowAndRun()
}

答案1

得分: 1

这段代码的意思是,对象是一个容器,所以通常你可以迭代v1.(*fyne.Container).Objects

然而,看起来你可以避免在代码中使用循环,因为按钮始终引用一个在添加按钮时设置的索引 - 所以你可以将当前的index(长度)传递给w1,然后递增它。

英文:

The object is a container, so in general you could iterate v1.(*fyne.Container).Objects.

However it looks like you could avoid looping in your code because the button always refers to an index that is set when the button is added - so you could pass the current index (length) to w1 and then increment it.

答案2

得分: 0

你不需要循环遍历**NewVBoxLayout,因为它是fyne.CanvasObject类型的。如果你想知道包含这个小部件NewButton的布局的索引,你可以通过在NewVScroll中搜索NewVBoxLayout**来实现。

以下是代码:

package main

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

var cEntries *fyne.Container

func FindObject(object fyne.CanvasObject, objects []fyne.CanvasObject) (int, bool) {
	for k1, v1 := range objects {
		if v1 == object {
			return k1, true
		}
	}
	return 0, false
}

func w1() *fyne.Container {
	wb := widget.NewButton("", nil)
	c := container.New(layout.NewVBoxLayout(), wb)
	wb.OnTapped = func() {
		index, isin := FindObject(c, cEntries.Objects)
		fmt.Println(index, isin)
	}

	return c
}

func main() {
	a := app.New()
	w := a.NewWindow("")

	wbAdd := widget.NewButton("+", nil)
	cEntries = container.New(layout.NewVBoxLayout(), wbAdd, w1())

	wbAdd.OnTapped = func() {
		cEntries.Add(w1())
	}

	wsEntries := container.NewVScroll(cEntries)

	w.SetContent(wsEntries)
	w.ShowAndRun()
}

在这段代码中,你创建了一个处理按钮的容器,然后你搜索包含该按钮的容器。

英文:

you don't need to loop over NewVBoxLayout because it is fyne.CanvasObject then if you want to know the index of the layout that contain this widget NewButton you can do it by just search for NewVBoxLayout in NewVScroll
this is the code

package main

import (
	"fmt"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/layout"
	"fyne.io/fyne/v2/widget"
)

var cEntries *fyne.Container

func FindObject(object fyne.CanvasObject, objects []fyne.CanvasObject) (int, bool) {
	for k1, v1 := range objects {
		if v1 == object {
			return k1, true
		}
	}
	return 0, false
}

func w1() *fyne.Container {
	wb := widget.NewButton("", nil)
	c := container.New(layout.NewVBoxLayout(), wb)
	wb.OnTapped = func() {
		index, isin := FindObject(c, cEntries.Objects)
		fmt.Println(index, isin)
	}

	return c
}

func main() {
	a := app.New()
	w := a.NewWindow("")

	wbAdd := widget.NewButton("+", nil)
	cEntries = container.New(layout.NewVBoxLayout(), wbAdd, w1())

	wbAdd.OnTapped = func() {
		cEntries.Add(w1())
	}

	wsEntries := container.NewVScroll(cEntries)

	w.SetContent(wsEntries)
	w.ShowAndRun()
}

see in this code you make the container that handle the button and then you search for the container that contain this button

huangapple
  • 本文由 发表于 2022年5月18日 17:43:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/72286754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定