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

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

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

问题

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

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/rivo/tview"
  5. )
  6. func main() {
  7. app := tview.NewApplication()
  8. form := tview.NewForm()
  9. form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft)
  10. form.AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil)
  11. form.AddInputField("Value1", "", 0, nil, nil)
  12. form.AddInputField("Value2", "", 0, nil, nil)
  13. form.AddInputField("Value3", "", 0, nil, nil)
  14. form.AddInputField("Value4", "", 0, nil, nil)
  15. form.AddButton("OK", func() { app.Stop() })
  16. if err := app.SetRoot(form, true).SetFocus(form).Run(); err != nil {
  17. panic(err)
  18. }
  19. fmt.Printf("%s\n", form.GetFormItem(0).(*tview.DropDown))
  20. }

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

英文:

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

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/rivo/tview"
  5. )
  6. func main() {
  7. app := tview.NewApplication()
  8. form := tview.NewForm()
  9. form.SetBorder(true).SetTitle("Enter some data").SetTitleAlign(tview.AlignLeft)
  10. form.AddDropDown("Title", []string{"Mr.", "Ms.", "Mrs.", "Dr.", "Prof."}, 0, nil)
  11. form.AddInputField("Value1", "", 0, nil, nil)
  12. form.AddInputField("Value2", "", 0, nil, nil)
  13. form.AddInputField("Value3", "", 0, nil, nil)
  14. form.AddInputField("Value4", "", 0, nil, nil)
  15. form.AddButton("OK", func() { app.Stop() })
  16. if err := app.SetRoot(form, true).SetFocus(form).Run(); err != nil {
  17. panic(err)
  18. }
  19. fmt.Printf("%s\n", form.GetFormItem(0).(*tview.DropDown))
  20. }

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

答案1

得分: 1

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

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

示例输出:

  1. 2 Mrs.
英文:

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

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

Example output:

  1. 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:

确定