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.
使用方法
程序基本结构
func main() {
myApp := app.New()
//新建窗口
myWindow := myApp.NewWindow("Hello")
//指定窗口内容
myWindow.SetContent(widget.NewLabel("Hello"))
myWindow.Show()
myApp.Run()
//上两句话与myWindow.ShowAndRun()等同
}
App.Quit(): 退出程序
package main
import (
"time"
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/widget"
)
func main() {
myApp := app.New()
myWindow := myApp.NewWindow("Hello")
myWindow.SetContent(widget.NewLabel("Hello"))
go showAnother(myApp)
myWindow.ShowAndRun()
}
func showAnother(a fyne.App) {
time.Sleep(time.Second * 5)
win := a.NewWindow("Shown later")
win.SetContent(widget.NewLabel("5 seconds later"))
win.Resize(fyne.NewSize(200, 200))
win.Show()
time.Sleep(time.Second * 2)
win.Hide()
}
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) {}) 下拉框
表单
form := &widget.Form{
Items: []*widget.FormItem{ // 表单中的控件
{"Entry", entry}},
OnSubmit: func() { // 提交按钮
...
},
OnCancel: func() { // 取消按钮
...
},
}
// 添加表单中的元素
form.Append("Text", textArea)
进度条
widget.NewProgressBar() 默认最小值0.0 最大值1.1
widget.NewProgressBarInfinite() 无穷进度条
属性页
tabs := widget.NewTabContainer(
widget.NewTabItem("Tab 1", widget.NewLabel("Hello")),
widget.NewTabItem("Tab 2", widget.NewLabel("World!")))
//widget.NewTabItemWithIcon("Home", theme.HomeIcon(), widget.NewLabel("Home tab"))
//设置属性页的位置
tabs.SetTabLocation(widget.TabLocationLeading)
工具栏
toolbar := widget.NewToolbar(
widget.NewToolbarAction(theme.DocumentCreateIcon(), func() {
log.Println("New document")
}),
widget.NewToolbarSeparator(),
widget.NewToolbarAction(theme.ContentCutIcon(), func() {}),
widget.NewToolbarAction(theme.ContentCopyIcon(), func() {}),
widget.NewToolbarAction(theme.ContentPasteIcon(), func() {}),
widget.NewToolbarSpacer(),
widget.NewToolbarAction(theme.HelpIcon(), func() {
log.Println("Display help")
}),
)