英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论