主要()不等待使用fyne进行文件对话框的更新的golang中的文件对话框。

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

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()
}

huangapple
  • 本文由 发表于 2021年8月11日 16:50:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/68738840.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定