英文:
GUI via Fyne: changing elements on app page on button clicking
问题
在点击按钮时更改应用程序页面上的元素的最佳实践是什么?
例如,我有以下代码:
package main
import (
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)
func main() {
	a := app.New()
	w := a.NewWindow("Hello")
	hello := widget.NewLabel("Hello Fyne!")
	w.SetContent(container.NewVBox(
		hello,
		widget.NewButton("Hi!", func() {
			// do something
		}),
	))
	w.ShowAndRun()
}
如果点击NewButton,我想要在窗口上更改元素,并显示具有不同功能的新按钮。
英文:
What is the best practice to change elements on an app page at button clicking.
For example, I have such code
package main
import (
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
)
func main() {
	a := app.New()
	w := a.NewWindow("Hello")
	hello := widget.NewLabel("Hello Fyne!")
	w.SetContent(container.NewVBox(
		hello,
		widget.NewButton("Hi!", func() {
			// do something
		}),
	))
	w.ShowAndRun()
}
I want to change elements on this window if clicking on a NewButton. And display a new Buttons with different functions at their clicking
答案1
得分: 1
如果您想更改容器的内容,您需要将容器设置为一个变量,以便稍后可以访问其方法和字段来操作内容。
content := container.NewVBox(…)
w.SetContent(content)
然后,您可以使用content上的方法或更改其Objects字段,然后调用Refresh()。
英文:
If you want to change the content of a container you will want to set the Container to a variable so you can access its methods and fields later to manipulate the content.
content := container.NewVBox(…)
w.SetContent(container)
Then you can use methods on content or change its Objects field then call Refresh().
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论