Simple try of golang gui library fyne

fyne is a GUI library written based on go, which can run on desktop and mobile. The style is Material Design, and its github address is https://github.com/fyne-io/fyne

This article mainly describes how to initially use fyne under Windows


One install

1. Install golang

The version must be greater than 1.12. You can search for various tutorials on the Internet.

2. Install fyne

Go to the address https://github.com/fyne-io/fyne/releases to download the latest release of fyne version. The latest version is 1.24 at the time of writing. After downloading, unzip it. The total is about 30M.
Insert picture description here
Put the folder fyne-1.24 Change to fyne, then create a new folder called fyne-io under the go installation path C:\Go\src, and then copy the fyne folder to fyne-io

3. Install 64-bit MinGW

The official website says that because fyne needs to use gcc to communicate with the system's graphics driver, it needs to install gcc, and just use MinGW under win.

You can go to this address https://jmeubank.github.io/tdm-gcc/download/ to download 64-bit MinGW for installation.

I have installed CodeBlocks 20.03 and MinGW by the way, so I only need to add the bin path under MinGW to the system variables.
Insert picture description here


Two easy to use

After the installation is complete, you can have a simple experience of fyne, a simple example main.go is as follows,

// main.go
package main

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

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

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

	w.ShowAndRun()
}

Use the command go run main.go on the command line, and the results are as follows.
Insert picture description here
This will run successfully. The first run will be very slow. It is estimated that some programs are compiled using MinGW, and it will be very fast later.

A more complicated example is as follows,

// Package main provides various examples of Fyne API capabilities
package main

import (
	"fmt"
	"net/url"

	"fyne.io/fyne"
	"fyne.io/fyne/app"
	"fyne.io/fyne/canvas"
	"fyne.io/fyne/cmd/fyne_demo/data"
	"fyne.io/fyne/cmd/fyne_demo/screens"
	"fyne.io/fyne/layout"
	"fyne.io/fyne/theme"
	"fyne.io/fyne/widget"
)

const preferenceCurrentTab = "currentTab"

func parseURL(urlStr string) *url.URL {
    
    
	link, err := url.Parse(urlStr)
	if err != nil {
    
    
		fyne.LogError("Could not parse URL", err)
	}

	return link
}

func welcomeScreen(a fyne.App) fyne.CanvasObject {
    
    
	logo := canvas.NewImageFromResource(data.FyneScene)
	logo.SetMinSize(fyne.NewSize(228, 167))

	return widget.NewVBox(
		widget.NewLabelWithStyle("Welcome to the Fyne toolkit demo app", fyne.TextAlignCenter, fyne.TextStyle{
    
    Bold: true}),
		layout.NewSpacer(),
		widget.NewHBox(layout.NewSpacer(), logo, layout.NewSpacer()),

		widget.NewHBox(layout.NewSpacer(),
			widget.NewHyperlink("fyne.io", parseURL("https://fyne.io/")),
			widget.NewLabel("-"),
			widget.NewHyperlink("documentation", parseURL("https://fyne.io/develop/")),
			widget.NewLabel("-"),
			widget.NewHyperlink("sponsor", parseURL("https://github.com/sponsors/fyne-io")),
			layout.NewSpacer(),
		),
		layout.NewSpacer(),

		widget.NewGroup("Theme",
			fyne.NewContainerWithLayout(layout.NewGridLayout(2),
				widget.NewButton("Dark", func() {
    
    
					a.Settings().SetTheme(theme.DarkTheme())
				}),
				widget.NewButton("Light", func() {
    
    
					a.Settings().SetTheme(theme.LightTheme())
				}),
			),
		),
	)
}

func main() {
    
    
	a := app.NewWithID("io.fyne.demo")
	a.SetIcon(theme.FyneLogo())

	w := a.NewWindow("Fyne Demo")
	w.SetMainMenu(fyne.NewMainMenu(fyne.NewMenu("File",
		fyne.NewMenuItem("New", func() {
    
     fmt.Println("Menu New") }),
		// a quit item will be appended to our first menu
	), fyne.NewMenu("Edit",
		fyne.NewMenuItem("Cut", func() {
    
     fmt.Println("Menu Cut") }),
		fyne.NewMenuItem("Copy", func() {
    
     fmt.Println("Menu Copy") }),
		fyne.NewMenuItem("Paste", func() {
    
     fmt.Println("Menu Paste") }),
	)))
	w.SetMaster()

	tabs := widget.NewTabContainer(
		widget.NewTabItemWithIcon("Welcome", theme.HomeIcon(), welcomeScreen(a)),
		widget.NewTabItemWithIcon("Widgets", theme.ContentCopyIcon(), screens.WidgetScreen()),
		widget.NewTabItemWithIcon("Graphics", theme.DocumentCreateIcon(), screens.GraphicsScreen()),
		widget.NewTabItemWithIcon("Windows", theme.ViewFullScreenIcon(), screens.DialogScreen(w)),
		widget.NewTabItemWithIcon("Advanced", theme.SettingsIcon(), screens.AdvancedScreen(w)))
	tabs.SetTabLocation(widget.TabLocationLeading)
	tabs.SelectTabIndex(a.Preferences().Int(preferenceCurrentTab))
	w.SetContent(tabs)

	w.ShowAndRun()
	a.Preferences().SetInt(preferenceCurrentTab, tabs.CurrentTabIndex())
}

Run it again, you can see the following interface, dark mode,
Insert picture description here
click Light at the bottom right to switch to bright mode,
Insert picture description here
you can also click in other places to see the parts provided by fyne.


Three summary

Generally speaking, fyne is still good. At the time of writing this article, the star on github is 9.5k, which is quite high. Since go is the main server development, the gui aspect is relatively weak, and fyne just makes up for this.

I personally feel that its advantage is small, only about 30M. It should be no problem in building small and medium-sized. Moreover, GUI needs a background to handle various events on the UI. It happens to be that go is good at it and can be seamlessly connected. However, compared with QT, there is still a big gap, but QT is getting bigger and bigger now...

If there is something wrong with the writing, I hope to leave a message to correct it, thank you for reading.

Guess you like

Origin blog.csdn.net/whahu1989/article/details/105874308