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