Custom App Icon – Fyne GUI Golang tutorial 25

Hey, In this tutorial we are going to learn. How to change App ICON in Fyne.

Though it is very easy.

How to change fyne App icon in windows title bar?

You can use the code give below. Where “w” is variable for window.

[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”]w.SetIcon(theme.fyneLogo())[/sourcecode]

How Load custom assets/images in fyne?

You can use load resource function. For example

[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”]fyne.LoadResourceFromPath("c:/assets/cat.png")[/sourcecode]

Where “r” is a variable short for resource. You can use any.

use this ‘r’ variable to change app icon. For example

[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”]w.SetIcon(r)[/sourcecode]

How to Load image from internet in Fyne & Golang?
You can use “Load Resource From URL STRING” function.

[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”]r,_:= fyne.LoadResourceFromURLString(urlStr)[/sourcecode]

Just provide the url and call the function. For Example

[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”]r,_:= LoadResourceFromURLString("https://picsum.photos/200")[/sourcecode]

Where “_” is the error. Underscore mean to ignore the value of error here.

so You can use it change icon like given below.

[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”]w.SetIcon(r)[/sourcecode]

Tricky part

The tricky part is use custom image in the icon place holder.

Both source code and resource file is uploaded. which is used for custom icon.

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

[easy_media_download url=”http://blogvali.com/wp-content/uploads/fyne-golang-downloads/reso.go” text=”Resource” color=”red_darker”] [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

// import fyne
import (
"io/ioutil"
"net/http"
"path/filepath"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
)

func main() {
//New App
a := app.New()
// New Window
w := a.NewWindow("How to change App ICON")
// New title bar
// New App ICON
// How to use custom ICON ?
r, _ := LoadResourceFromPath("c:/assets/cat.png")
w.SetIcon(r)
// Resize
w.Resize(fyne.NewSize(400, 400))
// Show and run
w.ShowAndRun()
}

// //////// Paste Code here
// Resource represents a single binary resource, such as an image or font.
// A resource has an identifying name and byte array content.
// The serialised path of a resource can be obtained which may result in a
// blocking filesystem write operation.
type Resource interface {
Name() string
Content() []byte
}

// StaticResource is a bundled resource compiled into the application.
// These resources are normally generated by the fyne_bundle command included in
// the Fyne toolkit.
type StaticResource struct {
StaticName string
StaticContent []byte
}

// Name returns the unique name of this resource, usually matching the file it
// was generated from.
func (r *StaticResource) Name() string {
return r.StaticName
}

// Content returns the bytes of the bundled resource, no compression is applied
// but any compression on the resource is retained.
func (r *StaticResource) Content() []byte {
return r.StaticContent
}

// NewStaticResource returns a new static resource object with the specified
// name and content. Creating a new static resource in memory results in
// sharable binary data that may be serialised to the location returned by
// CachePath().
func NewStaticResource(name string, content []byte) *StaticResource {
return &StaticResource{
StaticName: name,
StaticContent: content,
}
}

// LoadResourceFromPath creates a new StaticResource in memory using the contents of the specified file.
func LoadResourceFromPath(path string) (Resource, error) {
bytes, err := ioutil.ReadFile(filepath.Clean(path))
if err != nil {
return nil, err
}
name := filepath.Base(path)
return NewStaticResource(name, bytes), nil
}

// LoadResourceFromURLString creates a new StaticResource in memory using the body of the specified URL.
func LoadResourceFromURLString(urlStr string) (Resource, error) {
res, err := http.Get(urlStr)
if err != nil {
return nil, err
}
defer res.Body.Close()
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
name := filepath.Base(urlStr)
return NewStaticResource(name, bytes), nil
}

[/sourcecode] [easy_media_download url=”http://blogvali.com/wp-content/uploads/fyne-golang-downloads/main25.go” text=”Download Code” color=”red_darker”]
[easy_media_download url=”http://blogvali.com/wp-content/uploads/fyne-golang-downloads/reso.go” text=”Resource” 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: