package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/widget"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("accordion 控件")
accordions(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
// 折叠面板
func accordions(w fyne.Window) {
accordion1 := widget.NewAccordion(
widget.NewAccordionItem("according 1", widget.NewButton("11", nil)),
widget.NewAccordionItem("according 1", widget.NewLabel("11")),
)
accordion1.MultiOpen = true // 展开多个
w.SetContent(accordion1)
}
2. 卡片 (card 控件)
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("acrd 卡片")
acrds(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func acrds(w fyne.Window) {
acrd1 := widget.NewCard("acrd", "12123", container.NewVBox(
widget.NewButton("clikc", nil),
widget.NewLabel("123")))
acrd1.Image = canvas.NewImageFromFile("1.jpg")
w.SetContent(acrd1)
}
3. 表单 (form 控件)
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("form 表单控件")
forms(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func forms(w fyne.Window) {
formitem1 := widget.NewEntry() // 输入框
formitem2 := widget.NewRadioGroup([]string{"a", "b"}, nil) // 单选框
formitem3 := widget.NewCheck("123", nil) // 多选框
formitem4 := widget.NewCheck("456", nil)// 多选框
formint1 := widget.NewFormItem("enrty", formitem1)// from单选项
formint2 := widget.NewFormItem("radio", formitem2)// from单选项
formint3 := widget.NewFormItem("check", container.NewHBox(formitem3,formitem4))// from单选项
form1 := widget.NewForm(formint1, formint2, formint3)// from
form1.OnSubmit = func() {// 提交
fmt.Println("sub")
}
form1.SubmitText = "yes" // 设置提交文字
form1.OnCancel = func() { // 取消
fmt.Println("cal")
}
form1.CancelText = "no" // 设置取消文字
form1.Items[0].HintText = "this is a form s" // 提示信息
w.SetContent(container.NewVBox(form1))
}
4. 列表 (list 控件)
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/flopp/go-findfont"
"strconv"
"time"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("list 控件 列表")
lists(c)
//listObjs(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
// list放入普通切片
func lists(w fyne.Window) {
//data := []string{"red", "blue", "yellow"} // 普通的切片
data := findfont.List() // 字体库切片
labelTatol := widget.NewLabel("")
labelTatols := widget.NewLabel("")
labelTatol.SetText(strconv.Itoa(len(data)))
list1 := widget.NewList(
func() int {
return len(data) // 数据长度
},
func() fyne.CanvasObject {
return widget.NewLabel("") //标签容器
//return widget.NewEntry() //输入框容器
},
func(id widget.ListItemID, object fyne.CanvasObject) {
//fmt.Println(id, object)
lable1 := object.(*widget.Label)
lable1.Text = data[id]
},
)
list1.Select(1) // 选中
list1.OnUnselected = func(id widget.ListItemID) {
fmt.Println(id, "---")
}
stopChan := make(chan struct{})
// 开启自动预览
buttonStart := widget.NewButton("start", func() {
tickers := time.NewTicker(time.Millisecond * 200)
go func() {
for i := 0; i < len(data); i++ {
fmt.Println(i, "--", len(data))
select {
case <-tickers.C:
list1.Select(i)
labelTatols.SetText(strconv.Itoa(i))
case <-stopChan:
return
}
}
}()
})
// 停止自动预览
buttonStop := widget.NewButton("stop", func() {
stopChan <- struct{}{}
})
//w.SetContent(list1,labelTatol)
w.SetContent(container.NewBorder(container.NewVBox(buttonStart, buttonStop), container.NewVBox(labelTatol, labelTatols), nil, nil, list1))
}
type person struct {
name, age string
}
// list方入结构体
func listObjs(w fyne.Window) {
data := []person{
person{"zhangsan", "13"},
person{"lisi", "24"},
person{"wangwu", "35"},
}
list1 := widget.NewList(
func() int {
return len(data) // 数据长度
},
func() fyne.CanvasObject {
return container.NewHBox(widget.NewLabel(""), widget.NewLabel("")) //标签容器
//return widget.NewEntry() //输入框容器
},
func(id widget.ListItemID, object fyne.CanvasObject) {
fmt.Println(id, object)
lable1 := object.(*fyne.Container)
leftLabel := lable1.Objects[0].(*widget.Label)
rightLabel := lable1.Objects[1].(*widget.Label)
leftLabel.Text = data[id].name
rightLabel.Text = data[id].age
},
)
list1.Select(1) // 选中
list1.OnUnselected = func(id widget.ListItemID) {
fmt.Println(id, "---")
}
w.SetContent(list1)
}
5. 菜单 (meun 控件)
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("meun 菜单")
meuns(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func meuns(w fyne.Window) {
label1 := widget.NewLabel("") // 标签控件
meun1 := widget.NewMenu(fyne.NewMenu("file", // widget 菜单控件
fyne.NewMenuItem("open", func() {
label1.Text = "file"
}),
fyne.NewMenuItem("save", func() {
label1.Text = "save"
}),
fyne.NewMenuItem("close", func() {
label1.Text = "close"
}),
))
meun2 := fyne.NewMenu("flie", // fyne 菜单控件
fyne.NewMenuItem("open", func() {
label1.Text = "file"
}),
fyne.NewMenuItem("save", func() {
label1.Text = "save"
}),
fyne.NewMenuItem("close", func() {
label1.Text = "close"
}),
)
meun3 := fyne.NewMenu("edit", // fyne 菜单控件
fyne.NewMenuItem("open", func() {
label1.Text = "file"
}),
fyne.NewMenuItem("save", func() {
label1.Text = "save"
}),
fyne.NewMenuItem("close", func() {
label1.Text = "close"
}),
)
mainMeun1 := fyne.NewMainMenu(meun2, meun3) // 把fyne菜单工具放到 主控件里面去
w.SetMainMenu(mainMeun1) // 设置 主菜单控件
w.SetContent(container.NewHBox(label1, meun1)) // 设置标签 widget控件
//fyne 包是一般是住程序包
//widget 包一般是组件包
}
6. 进度条 (progress 控件)
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"time"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("progress bar 控件")
bar(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func bar(w fyne.Window) {
bar1 := widget.NewProgressBar()
bar1.Value = 50
bar1.Min = 0
bar1.Max = 100
bar2 := widget.NewProgressBarInfinite()
btn := widget.NewButton("click", func() {
go func() {
for i := 0; i < 100; i++ {
bar1.SetValue(float64(i))
time.Sleep(time.Millisecond * 300)
fmt.Println(bar1.Value)
}
}()
})
btn2 := widget.NewButton("click 2", func() {
if bar2.Running() {
bar2.Stop()
} else {
bar2.Start()
}
})
w.SetContent(container.NewVBox(bar1, btn, bar2, btn2))
}
7. 下拉框 (select 控件)
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("select 控件")
selects(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func selects(w fyne.Window) {
label1 := widget.NewLabel("label") // 标签控件
select1 := widget.NewSelect([]string{"zhangsan", "lisi"}, func(s string) { // select控件
fmt.Println(s)
label1.Text = s
})
select1.PlaceHolder = "select form " // select 提示框
select2 := widget.NewSelectEntry([]string{"123", "456"}) // select 带输入框的控件
select2.OnSubmitted = func(s string) { // 输入框组件的提交实践
fmt.Println(s)
}
select3 := widget.NewSelect([]string{"1", "2", "3"}, nil)
select3.OnChanged = func(s string) {
fmt.Println(s)
}
select3.Selected = "2" // select 选中控件
w.SetContent(container.NewVBox(select1, label1, select2, select3))
}
8. 百分比条 (slider 控件)
package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"strconv"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("slider 控件")
sliders(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func sliders(w fyne.Window) {
label := widget.NewLabel("")
slider1 := widget.NewSlider(1, 100)
slider1.Step = 2 // 每次滚动的 步数
slider1.OnChanged = func(f float64) {
fmt.Println(f)
label.SetText(strconv.FormatFloat(f, 'f', 0, 64))
}
slider1.SetValue(50)
slider1.Orientation = widget.Vertical
w.SetContent(container.NewHBox(slider1,label))
}
发表评论