1、fyne初始化
新建项目目录 fyne
go mod init fyne
2、fyne依赖安装
go get fyne.io/fyne/v2
go mod tidy
3、gcc安装
fyne包含c/c++代码需要gcc编译,下载TDM-GCC,安装成功后默认设置了环境变量。
二、Hello Worldfyne默认没有中文字体,中文乱码。从系统目录C:\Windows\Fonts拷贝中文字体到fyne开发目录,并设置fyne字体 fyne_font。
package main
import (
"os"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
)
func main() {
// 设置中文字体,不然中文乱码
os.Setenv("FYNE_FONT", "msyhl.ttc")
a := app.New()
//创建一个窗口
w := a.NewWindow("gui编程")
//设置窗口内显示内容,需要中文字体
w.SetContent(widget.NewLabel("go GUI 编程"))
// 设置窗口大小
w.Resize(fyne.NewSize(400, 400))
// 窗口居中
w.CenterOnScreen()
//展示窗口 并运行程序
w.ShowAndRun()
}
go run main.go
fyne 绘制图像界面时钟
package main
import (
"fmt"
"image/color"
"math"
"time"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
)
const (
secondHandLenth float64 = 180.0
minuteHandLenth float64 = 120.0
hourHandLenth float64 = 80.0
)
func main() {
clockApp := app.New()
window := clockApp.NewWindow("时钟")
window.SetPadded(false)
yellow := color.RGBA{R: 0xff, G: 0xff, B: 0xcc, A: 0xff}
circle := canvas.NewCircle(yellow)
circle.StrokeWidth = 1
circle.StrokeColor = color.Black
// 圆所在正方形的左上角坐标
circle.Position1.X = 0
circle.Position1.Y = 0
// 圆所在正方形的右下角坐标
circle.Position2.X = 400
circle.Position2.Y = 400
layout := container.NewWithoutLayout(circle)
// 刻度
for i := 1; i <= 12; i++ {
scale := canvas.NewText(fmt.Sprint(i), color.Black)
scale.Alignment = fyne.TextAlignCenter
size := scale.MinSize()
// 根据字体大小计算刻度位置偏移量
h := size.Height / 2
//w := size.Width
// 移动刻度至合适位置
scale.Move(fyne.NewPos(getX(i*5, secondHandLenth), getY(i*5, secondHandLenth)-h))
layout.Add(scale)
}
now := time.Now()
second := now.Second()
secondHand := drawPointer(second, secondHandLenth)
layout.Add(secondHand)
minute := now.Minute()
minuteHand := drawPointer(minute, minuteHandLenth)
layout.Add(minuteHand)
hour := now.Hour() * 5
hourHand := drawPointer(hour, hourHandLenth)
layout.Add(hourHand)
window.SetContent(layout)
go func() {
for {
time.Sleep(1 * time.Second)
now := time.Now()
second := now.Second()
secondHand.Position2.X = getX(second, secondHandLenth)
secondHand.Position2.Y = getY(second, secondHandLenth)
secondHand.Refresh()
minute := now.Minute()
minuteHand.Position2.X = getX(minute, minuteHandLenth)
minuteHand.Position2.Y = getY(minute, minuteHandLenth)
minuteHand.Refresh()
hour := now.Hour() * 5
hourHand.Position2.X = getX(hour, hourHandLenth)
hourHand.Position2.Y = getY(hour, hourHandLenth)
hourHand.Refresh()
}
}()
window.Resize(fyne.NewSize(400, 400))
window.CenterOnScreen()
window.ShowAndRun()
}
// 绘制时钟指针
func drawPointer(second int, pointerLen float64) *canvas.Line {
pointerHand := canvas.NewLine(color.Black)
pointerHand.Position1.X = 200
pointerHand.Position1.Y = 200
pointerHand.Position2.X = getX(second, pointerLen)
pointerHand.Position2.Y = getY(second, pointerLen)
return pointerHand
}
func getX(second int, pointerLen float64) float32 {
return float32(200 + pointerLen*math.Cos(float64((math.Pi/float64(30))*float64(second)-(math.Pi/2))))
}
func getY(second int, pointerLen float64) float32 {
return float32(200 + pointerLen*math.Sin(float64((math.Pi/float64(30))*float64(second)-(math.Pi/2))))
}
运行结果如下图所示: