3

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

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
  • Have you studied the official Fyne demo program? Last time I checked, it was rather extensive in the actions implemented in its UI. – kostix Jan 10 '22 at 13:28
  • maybe [How to remove objects from golang fyne container](https://stackoverflow.com/q/63995289/10197418) is helpful? – FObersteiner Jan 13 '22 at 10:37
  • Maybe the “updating content” tutorial could help? https://youtu.be/h2ZOdTA3ew4 – andy.xyz Jan 13 '22 at 15:28

1 Answers1

1

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

andy.xyz
  • 2,567
  • 1
  • 13
  • 18