自学内容网 自学内容网

【Go系列】Go的UI框架GIO

        其实主要我是要花一个折线图,但是使用Fyne貌似画不出来,使用plot也没法动态生成,听说Gio可以,那就先介绍一下什么是Gio把。

GIO(gioui.org)是一个用于Go语言的跨平台GUI库,旨在为开发人员提供构建现代图形用户界面的工具。以下是关于GIO的一些关键点:

设计哲学

  • 跨平台:GIO旨在支持多个操作系统,包括Linux、Windows、macOS、Android和iOS。
  • 性能:GIO注重性能,目标是提供流畅的用户界面。
  • 声明式API:GIO使用声明式API来构建用户界面,这有助于简化代码和维护。

核心特性

  • 图形和绘图:GIO提供了丰富的图形和绘图工具,允许开发者绘制自定义图形、形状和文本。
  • 用户界面组件:GIO提供了多种UI组件,如按钮、文本框、滑动条等,用于构建交互式界面。
  • 事件处理:GIO能够处理各种用户输入事件,如鼠标点击、键盘输入和触摸操作。
  • 布局:GIO支持灵活的布局管理,允许开发者创建自适应的界面设计。
  • 动画和过渡:GIO支持动画效果,可以用来增强用户界面的交互性和视觉吸引力。

如何使用GIO

  • 安装:使用Go的包管理器go get来安装GIO库。

    bash

    复制

    go get -u gioui.org/io@latest
    
  • 构建应用:GIO应用通常从创建一个窗口开始,然后定义UI组件和布局,最后处理事件循环。

这边需要注意,新版的GIO改了很多接口,需要Go1.22开始才能运行。

示例代码

package main

import (
"fmt"
"image/color"
"log"
"os"

"gioui.org/app"
"gioui.org/op"
"gioui.org/text"
"gioui.org/widget/material"
)

func main() {
go func() {
window := new(app.Window)
err := run(window)
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}()
app.Main()
}

func run(window *app.Window) error {
theme := material.NewTheme()
theme.Bg = color.NRGBA{R: 0, G: 0, B: 0, A: 255}
var ops op.Ops
for {
switch e := window.Event().(type) {
case app.DestroyEvent:
fmt.Println("destroy")
return e.Err
case app.FrameEvent:
// This graphics context is used for managing the rendering state.
gtx := app.NewContext(&ops, e)

title := material.H1(theme, "Hello, Gio")

// Change the color of the label.
maroon := color.NRGBA{R: 127, G: 0, B: 0, A: 255}
title.Color = maroon

// Change the position of the label.
title.Alignment = text.Middle

// Draw the label to the graphics context.
title.Layout(gtx)

// Pass the drawing operations to the GPU.
e.Frame(gtx.Ops)
}
}
}


原文地址:https://blog.csdn.net/u013379032/article/details/140675986

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!