Home  >  Article  >  Backend Development  >  golang fyne gui cannot resize list

golang fyne gui cannot resize list

PHPz
PHPzforward
2024-02-05 23:45:03809browse

golang fyne gui 无法调整列表大小

Question content

I am trying to resize this (list) as it only displays one row and the second one sees it requires scrolling down I want to display multiple lines, this is the code:-

"fyne.io/fyne/v2/app"
    "fyne.io/fyne/v2/container"
    "fyne.io/fyne/v2/widget"
type CustomList struct {
    Header fyne.CanvasObject
    List   *widget.List
}
    a := app.New()

    // Create a new window
    w := a.NewWindow("Resources Manager")

    // Create a list with five columns
    list = widget.NewList(
        func() int {
            return len(clientInfos)
        },
        func() fyne.CanvasObject {
            return container.NewHBox(widget.NewIcon(nil), widget.NewLabel(""))
        },
        func(index int, obj fyne.CanvasObject) {
            c := obj.(*fyne.Container)
            icon := c.Objects[0].(*widget.Icon)
            label := c.Objects[1].(*widget.Label)

            // load the image resource
            img, err := fyne.LoadResourceFromPath(clientInfos[index].Country)
            if err != nil {
                fmt.Println("Failed to load image", err)
                return
            }

            // set image to icon and text to label
            icon.SetResource(img)
            label.SetText(fmt.Sprintf("%s | %s | %s | %s | %s",
                clientInfos[index].AppName,
                clientInfos[index].Version,
                clientInfos[index].kerenl,
                clientInfos[index].Price,
                clientInfos[index].Size,
            ))
            label.TextStyle = fyne.TextStyle{Bold: true, Italic: false, Monospace: true}
        },
    )
    list.OnSelected = func(id int) {
        selectedID = id
    }
    customList := &CustomList{
        Header: widget.NewLabel("Icon | AppName | kerenl | Price | Size | Status"),
        List:   list,
    }
    customList.List.Resize(fyne.Size{Height: 434})
    // Create a container for the buttons
    buttonContainer := container.NewVBox()

    buttonContainer.Add(widget.NewButton("Install", func() {
        // Handle button click
    }))
    buttonContainer.Add(widget.NewButton("download", func() {
        // Handle button click
    }))
    buttonContainer.Add(widget.NewButton("Upgrade", func() {
        // Handle button click
    }))
    buttonContainer.Add(widget.NewButton("Refresh", func() {
            list.Refresh()

        // Handle button click
    }))
    buttonContainer.Resize(fyne.NewSize(230, 300))
    vbox := container.NewVBox(
        customList.Header,
        customList.List,
    )
    vbox.Resize(fyne.NewSize(600, 320))


    
    horizontalSplit := container.NewHSplit(vbox, buttonContainer)
    horizontalSplit.SetOffset(0.8)

    content := container.NewVBox(horizontalSplit, textArea)

    w.SetContent(content)

    w.Resize(fyne.NewSize(800, 270))

    w.ShowAndRun()

Are there any other Golang libraries for gui? Because it looks like most of them are outdated, right? However, I really need something simpler to build a GUI like


Correct Answer


In fyne, the widgets are put into a container, and The container usually has a layout. If you set this up, your manual calls to Resize will be overridden by the selected layout. The use of VBox does exactly this, as its algorithm wants to keep each item as short as possible. Use borders instead, with the title at the top and the list taking up the remaining space.

Objects in Fyne always fill the space allocated to them, but to make it do what you want, it's important to choose the right container/layout. https://www.php.cn/link/4d6ce445727ef59cc07abb95d3e4a1d4

The above is the detailed content of golang fyne gui cannot resize list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete