Go语言开发GUI程序
Go语言开发GUI程序
简介
推荐跨平台的flyne来编写go语言的GUI程序,无任何依赖
赖得翻译了,直接贴文了。
About
Fyne is an easy-to-use UI toolkit and app API written in Go. It is designed to build applications that run on desktop and mobile devices with a single codebase.
Version 2.1 is the current release of the Fyne API, it introduced RichText and the DocTabs container, as well as the document storage API and FyneApp.toml metadata support. We are now working towards the next big release, codenamed bowmore and more news will follow in our news feeds and GitHub project.
Prerequisites
To develop apps using Fyne you will need Go version 1.12 or later, a C compiler and your system's development tools. If you're not sure if that's all installed or you don't know how then check out our Getting Started document.
Using the standard go tools you can install Fyne's core library using:
$ go get fyne.io/fyne/v2
中文乱码处理
// Package main provides various examples of Fyne API capabilities
package main
import (
"fmt"
"net/url"
"os"
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/canvas"
"fyne.io/fyne/cmd/fyne_demo/data"
"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() {
// Mac 查看中文字体 fc-list :lang=zh
// /System/Library/Fonts/STHeiti Medium.ttc
// 设置FYNE_FONT环境变量,解决中文乱码问题
os.Setenv("FYNE_FONT", "/System/Library/Fonts/STHeiti Medium.ttc")
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(), welcomeScreen(a)),
)
tabs.SetTabLocation(widget.TabLocationLeading)
tabs.SelectTabIndex(a.Preferences().Int(preferenceCurrentTab))
w.SetContent(tabs)
w.ShowAndRun()
a.Preferences().SetInt(preferenceCurrentTab, tabs.CurrentTabIndex())
}