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())
    }
    
    

  • 相关阅读:
    dotnetcharting 生成柱状图,饼图等统计图
    asp.net 前后台相互调用
    SQL基础和高级(语法格式)总结二
    自己动手JQuery插件开发
    SQL基础之创建数据库,表,架构
    SQL基础和高级(语法格式)总结一
    ado.net总结
    [Nescafé 26] 小猫爬山&&售货员的难题&&区间众数(随机化大法好)
    POJ2774 后缀自动机&后缀数组
    【方向学习】单片机
  • 原文地址:https://www.cnblogs.com/jiftle/p/15315482.html
Copyright © 2011-2022 走看看