【问题标题】:main () not waiting for update of file dialog in golang using fyne for file dialogmain() 不等待 golang 中使用 fyne 文件对话框更新文件对话框
【发布时间】:2021-08-11 17:35:29
【问题描述】:

我想使用文件对话框来选择一个目录,以便在 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中的文本em>-标签。

我是 Golang 新手,所以对于更有经验的 Go 程序员来说,我的问题可能很愚蠢。无论如何,我将不胜感激!

提前致谢,

文兹

【问题讨论】:

    标签: go fyne


    【解决方案1】:

    记住回调函数的要点是不要等待。在您的代码中,总是在用户选择任何文件夹之前返回 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() // 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()
    }
    

    【讨论】:

    • 嗨@Nick,非常感谢您的回复和帮助!来自 Python,我仍然需要学习一些更改。 :-)。
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-11
    相关资源
    最近更新 更多