Go语言Fyne库中的widget.list按钮操作

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

Go lang fyne widget.list button action

问题

嗨,我正在尝试解决这个谜题,我该如何为每个单独的按钮设置函数?这样它们就不会对我拥有的所有3个按钮执行相同的操作。

通过使用onSelected应该是可能的,我只是不知道如何正确实现。是的,我确实阅读了Fyne的文档;=)

我知道这些实际上不是按钮,但是它们应该具有与按钮相同的选项,只需使用onSelected

关于widget.list的Fyne文档:
https://developer.fyne.io/api/v2.0/widget/list.html

package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"

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

func main() {
	a := app.New()
	a.Settings().SetTheme(theme.DarkTheme())
	W := a.NewWindow("Application-OutSight")
	W.Resize(fyne.NewSize(640, 460))

	menuBar := []string{"Home", "App-Status", "Exit"}

	listView := widget.NewList(func() int {
		return len(menuBar)
	}, func() fyne.CanvasObject {
		return widget.NewLabel("template")
	}, func(id widget.ListItemID, o fyne.CanvasObject) {
		o.(*widget.Label).SetText(menuBar[id])
	})

	contenttext := widget.NewLabel("Welcome to this App")

	// 我想要的是这样的,但是不知道如何正确实现
	listView.OnSelected[0] = func(id widget.ListItemID) { //button 0
	}
	listView.OnSelected[1] = func(id widget.ListItemID) { //button 1
	}
	listView.OnSelected[2] = func(id widget.ListItemID) { //button 2
	}

	split := container.NewHSplit(
		listView,
		container.NewMax(contenttext),
	)
	split.Offset = 0.2
	W.SetContent(split)
	W.ShowAndRun()
}

希望这可以帮助到你!

英文:

Hey im trying to solve this puzzle how do i set func for every seperate button i have ? so it dosent do the same for alle 3 buttons i have

it should be possibe by using onselected i just cant figure how to get it right. and yes i did read the docs from Fyne ;=)

i know its not actual buttons, but they should have the same options as a button just by using the onselected

fyne doc regarding widget.list
https://developer.fyne.io/api/v2.0/widget/list.html


package main

import (
	"encoding/json"
	"fmt"
	"io/ioutil"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"


func main() {


	a := app.New()
	a.Settings().SetTheme(theme.DarkTheme())
	W := a.NewWindow("Application-OutSight")
	W.Resize(fyne.NewSize(640, 460))

	menuBar := []strings{"Home","App-Status", "Exit"}
	
	listView := widget.NewList(func() int {
		return len(menuBar)
	}, func() fyne.CanvasObject {
		return widget.NewLabel("template")
	}, func(id widget.ListItemID, o fyne.CanvasObject) {
		o.(*widget.Label).SetText = (menuBar[id])

	})

	contenttext := widget.NewLabel("Welcome to this App")
	
	//what i want is this, but dont know how to get right
	listView.OnSelected[0] = func(id widget.ListItemID) { //button 0
			}
	listView.OnSelected[1] = func(id widget.ListItemID) { //button 1
			}
	listView.OnSelected[2] = func(id widget.ListItemID) { //buttton2
			}
			

	split := (container.NewHSplit(
		listView,
		container.NewMax(contenttext),
	))
	split.Offset = 0.2
	W.SetContent(split)
	W.ShowAndRun()

}

答案1

得分: 1

widget.List.OnSelected有一个参数widget.ListItemID,它是一个整数。

你可以使用这个id来调用不同项目的函数

listView.OnSelected = func(id widget.ListItemID) {
  switch id {
	case 0:
	  //在这里调用按钮/列表项0的函数
	case 1:
      //在这里调用按钮/列表项1的函数
	case 2:
	  //在这里调用按钮/列表项2的函数
    default:

  }
}
英文:

widget.List.OnSelected has argument widget.ListItemID which is an int.

You can use this id to called function for different items

listView.OnSelected = func(id widget.ListItemID) {
  switch id {
	case 0:
	  //call button/list-item 0 func here
	case 1:
      //call button/list-item 1 func here
	case 2:
	  //call button/list-item 2 func here
    default:

  }
}

huangapple
  • 本文由 发表于 2022年10月7日 18:49:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/73986031.html
匿名

发表评论

匿名网友

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

确定