英文:
main () not waiting for update of file dialog in golang using fyne for file dialog
问题
我想在Go中使用文件对话框来选择一个目录,以便在后续使用中。为了选择目录,我正在使用Fyne文件对话框,因为应用程序的其余部分也使用了Fyne。我成功创建了一个简单的测试应用程序:
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
)
var save_dir string = "NoPathYet!"
func chooseDirectory(w fyne.Window) string {
dialog.ShowFolderOpen(func(dir fyne.ListableURI, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if dir != nil {
fmt.Println(dir.Path())
save_dir = dir.Path() // here value of save_dir shall be updated!
}
fmt.Println(save_dir)
}, w)
return save_dir
}
func main() {
a := app.New()
w := a.NewWindow("FileDialogTest")
hello := widget.NewLabel("Hello Fyne!")
w.SetContent(container.NewVBox(
hello,
widget.NewButton("Go Get Directory!", func() {
hello.SetText(chooseDirectory(w)) // Text of hello updated by return value
}),
))
w.Resize(fyne.NewSize(500, 500))
w.ShowAndRun()
}
它无法正常工作,标签hello在返回值之前被更新了,不知何故...
当点击按钮**"Go Get Directory!"**时,应调用函数chooseDirectory,并将返回值设置为hello标签中的文本。
我是一个Golang新手,所以我的问题对于有经验的Go程序员来说可能很愚蠢。无论如何,我都会感激帮助!
提前感谢,
Vinz
英文:
I would like to use a file dialog to select a directory for further use in Go. To select the directory i am using a Fyne file dialog as the rest of the application uses Fyne as well. I managed to create a simple test application:
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
)
var save_dir string = "NoPathYet!"
func chooseDirectory(w fyne.Window) string {
dialog.ShowFolderOpen(func(dir fyne.ListableURI, err error) {
if err != nil {
dialog.ShowError(err, w)
return
}
if dir != nil {
fmt.Println(dir.Path())
save_dir = dir.Path() // here value of save_dir shall be updated!
}
fmt.Println(save_dir)
}, w)
return save_dir
}
func main() {
a := app.New()
w := a.NewWindow("FileDialogTest")
hello := widget.NewLabel("Hello Fyne!")
w.SetContent(container.NewVBox(
hello,
widget.NewButton("Go Get Directory!", func() {
hello.SetText(chooseDirectory(w)) // Text of hello updated by return value
}),
))
w.Resize(fyne.NewSize(500, 500))
w.ShowAndRun()
}
It does not work properly, the label hello is updated prior to the return of the value, somehow...
When the button "Go Get Directory!" is clicked the function chooseDirectory should called and the return value should be set as text in the hello-Label.
I am a Golang-Newbie, so my question may be silly for more experienced Go programmers. In any case I would appreciate help!
Thanks in advance,
Vinz
答案1
得分: 5
记住回调函数的目的不是等待。在你的代码中,chooseDirectory(w)
总是在用户选择任何文件夹之前返回。所以,你应该直接在ShowFolderOpen
的回调函数中更新hello
标签的文本。
以下是预期工作的代码。
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
)
func chooseDirectory(w fyne.Window, h *widget.Label) {
dialog.ShowFolderOpen(func(dir fyne.ListableURI, err error) {
save_dir := "NoPathYet!"
if err != nil {
dialog.ShowError(err, w)
return
}
if dir != nil {
fmt.Println(dir.Path())
save_dir = dir.Path() // 这里应该更新 save_dir 的值!
}
fmt.Println(save_dir)
h.SetText(save_dir)
}, w)
}
func main() {
a := app.New()
w := a.NewWindow("FileDialogTest")
hello := widget.NewLabel("Hello Fyne!")
w.SetContent(container.NewVBox(
hello,
widget.NewButton("Go Get Directory!", func() {
chooseDirectory(w, hello) // hello 的文本通过返回值更新
}),
))
w.Resize(fyne.NewSize(500, 500))
w.ShowAndRun()
}
英文:
Remember the point of callback function is not to wait. In your code the chooseDirectory(w)
is always returned before the user selects any folder. So, you should directly update hello
label text within the callback of ShowFolderOpen
.
Here is working code as expected.
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/widget"
)
func chooseDirectory(w fyne.Window, h *widget.Label) {
dialog.ShowFolderOpen(func(dir fyne.ListableURI, err error) {
save_dir := "NoPathYet!"
if err != nil {
dialog.ShowError(err, w)
return
}
if dir != nil {
fmt.Println(dir.Path())
save_dir = dir.Path() // here value of save_dir shall be updated!
}
fmt.Println(save_dir)
h.SetText(save_dir)
}, w)
}
func main() {
a := app.New()
w := a.NewWindow("FileDialogTest")
hello := widget.NewLabel("Hello Fyne!")
w.SetContent(container.NewVBox(
hello,
widget.NewButton("Go Get Directory!", func() {
chooseDirectory(w, hello) // Text of hello updated by return value
}),
))
w.Resize(fyne.NewSize(500, 500))
w.ShowAndRun()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论