Code

The basic Hello World application with Fyne is quite succinct, as the application setup is encapsulated in a single call. The entry point, app.New(), provided by the app sub-package, sets up a new application that we use to open a new window. The widget sub-package defines the various widgets available that we can add to our new window:

package main

import "fyne.io/fyne/app"
import "fyne.io/fyne/widget"

func main() {
app := app.New()

win := app.NewWindow("Hello World")
win.SetContent(widget.NewVBox(
widget.NewLabel("Hello World!"),
widget.NewButton("Quit", func() {
app.Quit()
}),
))

win.ShowAndRun()
}

As you can see in the preceding code block, the newly created fyne.Window has its content set to a new widget.VBox that provides the basic layout. Into this, we add a Hello World! label using widget.NewLabel() and a Quit button using widget.NewButton(). The second parameter to the button is func(), which will be called when the button is tapped.

Lastly, we call ShowAndRun() on the window we created. This function will show the window and start the application event loop. It is shorthand for win.Show(); app.Run().

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.54.255