登录
首页 >  Golang >  Go问答

操作Go lang fyne widget.list 按钮

来源:stackoverflow

时间:2024-02-11 13:42:23 360浏览 收藏

最近发现不少小伙伴都对Golang很感兴趣,所以今天继续给大家介绍Golang相关的知识,本文《操作Go lang fyne widget.list 按钮》主要内容涉及到等等知识点,希望能帮到你!当然如果阅读本文时存在不同想法,可以在评论中表达,但是请勿使用过激的措辞~

问题内容

嘿,我正在尝试解决这个难题,如何为我拥有的每个单独的按钮设置功能?所以它不会对我拥有的所有 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 := []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()

}

正确答案


widget.list.onselected 的参数 widget.listitemid 是一个 int。

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

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:

  }
}

好了,本文到此结束,带大家了解了《操作Go lang fyne widget.list 按钮》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

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