zoukankan      html  css  js  c++  java
  • 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())
    }
    
    

  • 相关阅读:
    最短路+线段交 POJ 1556 好题
    判断线段和直线相交 POJ 3304
    nginx配置pathinfo模式,解决访问404
    使用ORM关联关系,如何自己关联自己
    PHPCMS
    linux安装redis服务,配置PHP扩展
    后台银行卡算法
    静态类和非静态类
    PHP的闭包和匿名函数
    php获取前一天时间段,每个月的第一天到最后一天
  • 原文地址:https://www.cnblogs.com/jiftle/p/15315482.html
Copyright © 2011-2022 走看看