package main
import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/data/binding"
"fyne.io/fyne/v2/widget"
"strconv"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("tree-list")
bindString(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
// 数据双休绑定 string
func bindString(w fyne.Window) {
db := binding.NewString()
label1 := widget.NewLabelWithData(db)
entry1 := widget.NewEntryWithData(db)
w.SetContent(container.NewVBox(label1, entry1))
}
// 数据双休绑定 stringList
func bindingList(w fyne.Window) {
datalist := binding.NewStringList()
list1 := widget.NewListWithData(
datalist,
func() fyne.CanvasObject {
return widget.NewLabel("")
}, func(item binding.DataItem, object fyne.CanvasObject) {
i := item.(binding.String)
txt, _ := i.Get()
label1 := object.(*widget.Label)
label1.SetText(txt)
})
lable1 := widget.NewLabel("")
i := 0
btn := widget.NewButton("add item", func() {
datalist.Append("red" + strconv.Itoa(i))
i++
})
w.SetContent(container.NewBorder(container.NewVBox(lable1, btn), nil, nil, nil, list1))
}
type userinfo struct {
Name string
Age int
}
// 数据双休绑定 结构体
func bandingStruct(w fyne.Window) {
label1 := widget.NewLabel("")
user1 := userinfo{
Name: "zhangsan",
Age: 20,
}
bandingStruct1 := binding.BindStruct(&user1)
btn := widget.NewButton("info", func() {
name, _ := bandingStruct1.GetValue("Name")
fmt.Println(name,"*----")
n := name.(string)
label1.SetText(n)
})
w.SetContent(container.NewVBox(btn, label1))
}
2. 表格控件 (table 控件)
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("tree-list")
tables(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func tables(w fyne.Window) {
data := [][]string{
{"name1", "age1", "sex1"},
{"name2", "age2", "sex2"},
{"name3", "age3", "sex3"},
{"name4", "age4", "sex4"},
}
label1 := widget.NewLabel("")
table1 := widget.NewTable(nil, nil, nil)
table1.Length = func() (int, int) {
return len(data), len(data[0])
}
table1.CreateCell = func() fyne.CanvasObject {
return widget.NewLabel("table")
}
table1.UpdateCell = func(id widget.TableCellID, template fyne.CanvasObject) {
label1 := template.(*widget.Label)
label1.SetText(data[id.Row][id.Col])
}
table1.OnSelected = func(id widget.TableCellID) {
//label1.SetText(strconv.Itoa(id.Row) + "," + strconv.Itoa(id.Col))
label1.SetText(data[id.Row][id.Col])
}
c := container.NewBorder(label1, nil, nil, nil, table1)
w.SetContent(c)
}
3. 树形 (tree 控件)
package main
import (
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"io/ioutil"
"os"
"path/filepath"
)
func main() {
MyApp := app.New()
c := MyApp.NewWindow("tree-list")
treeLists(c)
c.Resize(fyne.NewSize(600, 600))
c.ShowAndRun()
}
func treeLists(w fyne.Window) {
root := ".."
rootTxt := widget.NewEntry()
rootTxt.SetText(root)
tree1 := widget.NewTree(nil, nil, nil, nil)
button1 := widget.NewButton("tree click", func() {
refreshTree(tree1,rootTxt.Text)
})
w.SetContent(container.NewBorder(container.NewVBox(button1,rootTxt), nil, nil, nil, tree1))
}
func refreshTree(tree1 *widget.Tree,root string) {
tree1.Root = root
tree1.ChildUIDs = func(uid widget.TreeNodeID) (c []widget.TreeNodeID) {
if uid == "" {
c = getFileList(root)
} else {
c = getFileList(uid)
}
return
}
tree1.IsBranch = func(uid widget.TreeNodeID) (ok bool) {
return isDir(uid)
}
tree1.CreateNode = func(branch bool) (o fyne.CanvasObject) { // tree 添加控件的
return widget.NewLabel("")
}
tree1.UpdateNode = func(uid widget.TreeNodeID, branch bool, node fyne.CanvasObject) {
lbl := node.(*widget.Label)
file, err := os.Stat(uid)
if err != nil {
lbl.SetText("")
}
lbl.SetText(file.Name())
}
tree1.Refresh() // 刷新tree
}
func isDir(paths string) bool {
f, err := os.Stat(paths)
if err != nil {
return false
}
return f.IsDir()
}
// 得出当前目录内容
func getFileList(paths string) (lst []string) {
f, err := ioutil.ReadDir(paths)
if err != nil {
return
}
for _, file := range f {
path1 := paths + string(filepath.Separator) + file.Name()
lst = append(lst, path1)
}
return
}
发表评论