fyne.io/fyne

基本的公共定义,包括数据类型和接口
This import provides the basic definitions common to all Fyne code
including data types and interfaces.

fyne.io/fyne/app

程序起始函数,如app.New()
The app package provides the APIs that start a new application.
Normally you only require app.New().

fyne.io/fyne/canvas

绘制api
The canvas package provides all of the drawing APIs within Fyne.
The complete Fyne toolkit is made up of these primitive graphical types.

fyne.io/fyne/dialog

确认,错误对话框
Dialog windows such as confirm or error are handled by this package.

fyne.io/fyne/layout

用于布局的容器
The layout package provides various layout implementations for use
with containers (discussed in a later tutorial).

fyne.io/fyne/test

用于测试
Applications can be tested more easily using the tools within the test
package.

fyne.io/fyne/widget

工具与交互
Most graphical applications are created using a collection of widgets.
All the widgets and interactive elements within Fyne are in this package.

使用方法

程序基本结构

  1. func main() {
  2. myApp := app.New()
  3. //新建窗口
  4. myWindow := myApp.NewWindow("Hello")
  5. //指定窗口内容
  6. myWindow.SetContent(widget.NewLabel("Hello"))
  7. myWindow.Show()
  8. myApp.Run()
  9. //上两句话与myWindow.ShowAndRun()等同
  10. }

App.Quit(): 退出程序

  1. package main
  2. import (
  3. "time"
  4. "fyne.io/fyne"
  5. "fyne.io/fyne/app"
  6. "fyne.io/fyne/widget"
  7. )
  8. func main() {
  9. myApp := app.New()
  10. myWindow := myApp.NewWindow("Hello")
  11. myWindow.SetContent(widget.NewLabel("Hello"))
  12. go showAnother(myApp)
  13. myWindow.ShowAndRun()
  14. }
  15. func showAnother(a fyne.App) {
  16. time.Sleep(time.Second * 5)
  17. win := a.NewWindow("Shown later")
  18. win.SetContent(widget.NewLabel("5 seconds later"))
  19. win.Resize(fyne.NewSize(200, 200))
  20. win.Show()
  21. time.Sleep(time.Second * 2)
  22. win.Hide()
  23. }

Window.Resize(): 改变窗口大小

Canvas

矩形

canvas.NewRectangle(color.White)

文字

canvas.NewText(“Text Object”, color.White)
指定字体时应该使用.ttf文件

直线

canvas.NewLine(color.White)

圆形

canvas.NewCircle(color.White)

图像

canvas.NewImageFromFile(fileName)
canvas.NewImageFromResource(theme.FyneLogo())
canvas.NewImageFromImage(src)
image.FillMode可以设置为canvas.ImageFillStretch(充满指定空间),canvas.ImageFillContain(保持比例)和canvas.ImageFillOriginal(不小于图像原始大小)

Raster

canvas.NewRasterWithPixels()
canvas.NewRasterFromImage()
类似于图像,但绘图更加精细

渐变

canvas.NewHorizontalGradient(color.White, color.Transparent)
canvas.NewRadialGradient(color.White, color.Transparent)

Layout

fyne.NewContainerWithLayout(layout.NewHBoxLayout(),text1) 创建容器

其中,第一个参数有几个选项:

  • layout.NewHBoxLayout()(横向布局,所有元素高度相等)
  • layout.NewVBoxLayout()(竖向布局,所有元素宽度相等)
  • layout.NewGridLayout(n)(栅格布局,所有元素高宽相等)
  • layout.NewFixedGridLayout(fyne.NewSize(n, n))(固定栅格,行与列由容器的大小决定)
  • layout.NewBorderLayout(top, bottom, left, right)
  • layout.NewFormLayout()(类似于栅格,但栅格的高度和宽度由其中包含的控件决定)
  • layout.CenterLayout() (中心布局)
  • layout.NewMaxLayout()(所有控件都为同样大小,其大小取决于最大控件的最小尺寸)

后续参数可以使用layout.NewSpacer()填满所有可用的空间

Widget

文字

widget.NewLabel(“text”)

按钮

widget.NewButton(“click me”, func() {})
widget.NewButtonWithIcon(“Home”, theme.HomeIcon(), func() {})

可以通过fyne.LoadResourceFromPath()加载图标

容器

box :=widget.NewHBox()
box := widget.NewVBox()
box.append() 在容器后添加一个控件
box.Prepend() 在容器前添加一个控件

输入框

input := widget.NewEntry()
input := NewPasswordEntry()

input.SetPlaceHolder(“输入框默认值”)
imput.OnChanged = func(s string){} 每当输入款内容发生变化时触发事件
imput.SetReadOnly(true)

选择框

widget.NewCheck(“Optional”, func(value bool) {}) 复选框
widget.NewRadio([]string{“Option 1”, “Option 2”}, func(value string) {}) 单选框
widget.NewSelect([]string{“Option 1”, “Option 2”}, func(value string) {}) 下拉框

表单

  1. form := &widget.Form{
  2. Items: []*widget.FormItem{ // 表单中的控件
  3. {"Entry", entry}},
  4. OnSubmit: func() { // 提交按钮
  5. ...
  6. },
  7. OnCancel: func() { // 取消按钮
  8. ...
  9. },
  10. }
  11. // 添加表单中的元素
  12. form.Append("Text", textArea)

进度条

widget.NewProgressBar() 默认最小值0.0 最大值1.1
widget.NewProgressBarInfinite() 无穷进度条

属性页

  1. tabs := widget.NewTabContainer(
  2. widget.NewTabItem("Tab 1", widget.NewLabel("Hello")),
  3. widget.NewTabItem("Tab 2", widget.NewLabel("World!")))
  4. //widget.NewTabItemWithIcon("Home", theme.HomeIcon(), widget.NewLabel("Home tab"))
  5. //设置属性页的位置
  6. tabs.SetTabLocation(widget.TabLocationLeading)

工具栏

  1. toolbar := widget.NewToolbar(
  2. widget.NewToolbarAction(theme.DocumentCreateIcon(), func() {
  3. log.Println("New document")
  4. }),
  5. widget.NewToolbarSeparator(),
  6. widget.NewToolbarAction(theme.ContentCutIcon(), func() {}),
  7. widget.NewToolbarAction(theme.ContentCopyIcon(), func() {}),
  8. widget.NewToolbarAction(theme.ContentPasteIcon(), func() {}),
  9. widget.NewToolbarSpacer(),
  10. widget.NewToolbarAction(theme.HelpIcon(), func() {
  11. log.Println("Display help")
  12. }),
  13. )