我用fyne-io做了一个用户界面。在一个表格中,我希望一些单元格带有超链接,另一些带有标签。
我试过了,但没用:
package main
import (
"fmt"
"fyne.io/fyne"
"fyne.io/fyne/app"
"fyne.io/fyne/layout"
"fyne.io/fyne/widget"
)
func setDefaultColumnsWidth(table *widget.Table) {
table.SetColumnWidth(0, 130)
table.SetColumnWidth(1, 150)
table.SetColumnWidth(2, 160)
table.SetColumnWidth(3, 200)
table.SetColumnWidth(4, 400)
table.SetColumnWidth(5, 150)
table.SetColumnWidth(6, 250)
table.SetColumnWidth(7, 110)
table.SetColumnWidth(8, 80)
}
func main() {
application := app.New()
win := application.NewWindow("Test GUI")
data := [][]string{{"ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff"},
{"ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff"},
{"ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff", "ffffffffffffff"}}
tableData := widget.NewTable(
func() (int, int) {
return len(data), len(data[0])
},
func() fyne.CanvasObject {
label := widget.NewLabel("")
return label
},
func(i widget.TableCellID, o fyne.CanvasObject) {
switch o.(type) {
case *widget.Label:
label := o.(*widget.Label)
label.SetText(data[i.Row][i.Col])
case *widget.Hyperlink:
fmt.Println("Found Hyperlink")
hyperlink := o.(*widget.Hyperlink)
// hyperlink.SetText(data[i.Row][i.Col])
hyperlink.SetText("aaaaaaa")
}
})
setDefaultColumnsWidth(tableData)
id := widget.TableCellID{Row: 1, Col: 1}
hyperlink := widget.NewHyperlink("TUTU", nil)
obj := fyne.CanvasObject(hyperlink)
fmt.Println("UpdateCell...")
tableData.UpdateCell(id, obj)
label := widget.NewLabel("My table :")
button := widget.NewButton("Close", func() {
application.Quit()
})
container := layout.NewBorderLayout(label, button, nil, nil)
box := fyne.NewContainerWithLayout(container, label, tableData, button)
win.SetContent(box)
win.Resize(fyne.NewSize(1800, 400))
win.ShowAndRun()
}
我该怎么办?
我看到消息"fmt. Println("找到超链接")",但超链接未显示。
为什么o.(类型)等于 * widget. hyperlink而超链接不显示?
3条答案
按热度按时间9ceoxa921#
看起来问题是标签没有被破坏,并与新创建的超链接重叠。我无法找到上述问题的解决方案,但提出了替代解决方案,因为fyne API处于开发阶段,许多内部功能无法访问,可能会发生变化,解决方案可能会随着时间的推移而变化,并得到简化。
输出:
lskq00tm2#
我联系了FyneSlack小组,推荐的解决方案是将这两个元素封装在一个容器中,并在更新回调函数中只显示所需的元素
创建单元格回调函数:
更新回调函数:
myzjeezk3#
关于如何使用
container.NewMax
和Table.SetColumnWidth
创建更复杂的表,有一个更完整的说明。该示例逐步完成此表的代码:两个主要技巧是设置容器而不是单个小部件:
然后在回调中适当地隐藏或显示项目:
请访问https://fynelabs.com/2022/12/30/building-complex-tables-with-fyne/了解更多信息