class 4 fyne GoLang GUI Course BUTTON
Want to create button in fyne and golang? It is very easy.
You need to use
widget.NewButton()
You can pass two argument(values) to button.
- First one is name of button like
“MyButtonName” or
“JonyButton” or
“AliButton”
- Second one is the call back or function.
What the button will do when the button is pressed e.g func(){}
widget.NewButton("button Name", func() {})
You can assign the button widget to a variable( btn ) like the given below example.
btn := widget.NewButton("button Name", func() {})[sourcecode lang=”go” autolinks=”false” classname=”myclass” collapse=”false” firstline=”1″ gutter=”true” highlight=”1-3,6,9″ htmlscript=”false” light=”false” padlinenumbers=”false” smarttabs=”true” tabsize=”4″ toolbar=”false” title=”Source Code main.go”]
package main
// importing fyne
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
)
func main() {
// creating new app
a := app.New()
// New window
w := a.NewWindow("Here is my title for 4th tutorial")
// resizing my window
w.Resize(fyne.NewSize(400, 400))
// Now Its Time to use BUTTONs
// First value is button name
// 2nd value is any function
btn := widget.NewButton("button Name", func() {
// Our is ready
fmt.Println("Here is Go Button")
})
// using our button on
w.SetContent(btn)
// Running app
w.ShowAndRun()
}
Fyne GoLang GUI Course