尝试创建一个经典函数来调用文件、读取 csv、询问用户要保留哪些列,然后返回生成的最小化数据帧。
结果是对话框可以工作,但不会更新表的结果。它只是输出整个表格,就好像用户没有执行任何操作一样。
我尝试过使用 goroutine 来阻止执行,但是对话框也无法工作。事实上,整个程序永久冻结了。
如果您以前做过类似的事情,请不要费心阅读我所做的并尝试纠正它,请随时告诉我您将如何处理它。
(相关:我已经检查了main()不等待golang中使用fyne进行文件对话框更新文件对话框,但我不知道如何在此处直接执行
Set()
,因为返回类型更复杂。 )
这是我得到的:
func callFile(path string, w2 fyne.Window) (*widget.Table, *dataframe.DataFrame) {
file, err := os.Open(path)
if err != nil {
fmt.Println(err)
return nil, nil
}
csvDf := dataframe.ReadCSV(file)
NewDF := csvDf.Select(csvDf.Names())
table := widget.NewTable(
//[REMOVED TO SHORTEN THE POST]
)
//THIS NEXT SECTION IS ONLY RELEVANT TO MAKING THE DIALOG UI TEXT. FEEL FREE TO SKIP
---------------
coln := csvDf.Names()
typn := []string{}
for _, colName := range coln {
currcol := csvDf.Col(colName)
// Get the data type of the column
typn = append(typn, fmt.Sprint(currcol.Type()))
}
----------------
checks := []*widget.FormItem{}
boundChecks := make(map[string]bool)
for i, v := range coln {
//making the specific key for the column
staticref := v + " (" + fmt.Sprint(typn[i]) + ")"
boundChecks[staticref] = true
tempFI := widget.NewFormItem(staticref,
widget.NewCheck("", func(value bool) {
boundChecks[staticref] = value
}))
checks = append(checks, tempFI)
}
columnsToKeep := []string{}
dialog.ShowForm("Smart Select", "That should do it", "Close", checks, func(b bool) {
if b {
for i, v := range coln {
staticref := v + " (" + fmt.Sprint(typn[i]) + ")"
keep := boundChecks[staticref]
if keep {
// if the user checked this box
columnsToKeep = append(columnsToKeep, v)
}
}
NewDF = csvDf.Select(columnsToKeep)
table = ... // make the table again with NewDF instead of csvDf
} else {
// if the user doesn't check any boxes then all the columns will be selected
}
}, w2)
return table, &NewDF
}
如果数据发生变化,您不应该创建新表 - 您应该调用
table.Refresh()
,它会读取新数据。
如果这不起作用,那么其他海报是正确的,您的数据更新代码未按您的想法运行。