如何使用golang tview获取函数的文本值

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

How to get the text value of a function using golang tview

问题

在使用https://github.com/rivo/tview/中的表单内部获取函数值的方法尚不清楚。

package main

import (
    "fmt"
    "github.com/rivo/tview"
)

func main() {

    app := tview.NewApplication()
    form := tview.NewForm()
    form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft)
    form.AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil)
    form.AddInputField("Value1", "", 0, nil, nil)
    form.AddInputField("Value2", "", 0, nil, nil)
    form.AddInputField("Value3", "", 0, nil, nil)
    form.AddInputField("Value4", "", 0, nil, nil)
    form.AddButton("OK", func() { app.Stop() })
    if err := app.SetRoot(form, true).SetFocus(form).Run(); err != nil {
        panic(err)
    }
    fmt.Printf("%s\n", form.GetFormItem(0).(*tview.DropDown))
}

是否可以将下拉框的输出转换为文本?

英文:

It is not clear on how to get the value of the functions used inside of a form using https://github.com/rivo/tview/.

package main

import (
    "fmt"
    "github.com/rivo/tview"
)

func main() {

    app := tview.NewApplication()
    form := tview.NewForm()
    form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft)
    form.AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil)
    form.AddInputField("Value1", "", 0, nil, nil)
    form.AddInputField("Value2", "", 0, nil, nil)
    form.AddInputField("Value3", "", 0, nil, nil)
    form.AddInputField("Value4", "", 0, nil, nil)
    form.AddButton("OK", func() { app.Stop() })
    if err := app.SetRoot(form, true).SetFocus(form).Run(); err != nil {
        panic(err)
    }
    fmt.Printf("%s\n", form.GetFormItem(0).(*tview.DropDown))
}

Is it possible to convert the output of the dropdown to text?

答案1

得分: 1

使用DropDown.GetCurrentOption()方法来获取所选选项(索引和文本):

i, s := form.GetFormItem(0).(*tview.DropDown).GetCurrentOption()
fmt.Printf("%d %s\n", i, s)

示例输出:

2 Mrs.
英文:

To get the selected option (both index and text), use the DropDown.GetCurrentOption() method:

i, s := form.GetFormItem(0).(*tview.DropDown).GetCurrentOption()
fmt.Printf("%d %s\n", i, s)

Example output:

2 Mrs.

huangapple
  • 本文由 发表于 2021年12月28日 06:50:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/70501196.html
匿名

发表评论

匿名网友

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

确定