Button color, background, image – Fyne GUI Golang tutorial 44

Button color, background, image – Fyne GUI Golang tutorial 44

What is this tutorial about ?

Change fyne button color?

Fyne buttons are transparent.

there is no direct way to change color. But..

We are going to use a rectangle. If you change  color of rectangle. Button color will be changed.

 

Change fyne button background color?

If you place a color rectangle or any other shape. Button Background color will be change according to it.

 

Set image as Button background color

You can place image in the button background and Button will show image as a background.

 


package main
import (
    "image/color"
    "fyne.io/fyne/v2"
    "fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/canvas"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/layout"
    "fyne.io/fyne/v2/widget"
)
func main() {
    a := app.New()
    w := a.NewWindow("Colored Button")
    //resize window
    w.Resize(fyne.NewSize(400, 400))
    // Let show out button on screen
    w.SetContent(
        container.NewVBox(
            red_button(),
            red_button(),
            red_button(),
            red_button(),
            // let show / display buttons
            blue_button(),
            green_button(),
            img_button(),
        ),
    )
    w.ShowAndRun() // show and run app
}
// first colored button
func red_button() *fyne.Container { // return type
    btn := widget.NewButton("Visit", nil) // button widget
    // button color
    btn_color := canvas.NewRectangle(
        color.NRGBA{R: 255, G: 0, B: 0, A: 255})
    // container for colored button
    container1 := container.New(
        // layout of container
        layout.NewMaxLayout(),
        // first use btn color
        btn_color,
        // 2nd btn widget
        btn,
    )
    // our button is ready
    return container1
}
// Blue button
// copy previous button code and change it
func blue_button() *fyne.Container { // return type
    btn := widget.NewButton("Visit", nil) // button widget
    // button color
    btn_color := canvas.NewRectangle(
        color.NRGBA{R: 05, G: 0, B: 255, A: 255})
    // container for colored button
    container1 := container.New(
        // layout of container
        layout.NewMaxLayout(),
        // first use btn color
        btn_color,
        // 2nd btn widget
        btn,
    )
    // our button is ready
    return container1
}
// Green button
// copy and paste previous button
func green_button() *fyne.Container { // return type
    btn := widget.NewButton("Visit", nil) // button widget
    // button color
    btn_color := canvas.NewRectangle(
        color.NRGBA{R: 0, G: 255, B: 0, A: 255})
    // container for colored button
    container1 := container.New(
        // layout of container
        layout.NewMaxLayout(),
        // first use btn color
        btn_color,
        // 2nd btn widget
        btn,
    )
    // our button is ready
    return container1
}
// button with image as background
func img_button() *fyne.Container { // return type
    btn := widget.NewButton("Visit", nil) // button widget
    // img button color
    img := canvas.NewImageFromFile("c:/cartoon/blue-bg.jpg")
    // container for colored button
    container1 := container.New(
        // layout of container
        layout.NewMaxLayout(),
        // first use btn color
        img,
        // 2nd btn widget
        btn,
    )
    // our button is ready
    return container1
}

 

[easy_media_download url=”http://blogvali.com/wp-content/uploads/fyne-golang-downloads/main44.go” text=”Download Code” color=”red_darker”]

Fyne Golang GUI Course

 

Tony BB
 

TonyBB is a Coach , marketer, hypnotist and a founder of RSKVF Production who specializes in providing simple, affordable, and easy to use solutions for Life.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: