GUI via Fyne: changing elements on app page on button clicking

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


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