首页 > 解决方案 > 控制 GTK 小部件大小

问题描述

我想要一个 GTK 应用程序,它作为第一次迭代应该有一个长列表、一个隐藏的小部件和一个底线。所有这些都应该垂直布局。

到目前为止,我尝试使用 expand 方法执行此操作,但似乎没有任何效果:

package main

import (
    "log"

    "github.com/gotk3/gotk3/glib"
    "github.com/gotk3/gotk3/gtk"
)

func main() {
    // Initialize GTK without parsing any command line arguments.
    gtk.Init(nil)

    // Create a new toplevel window, set its title, and connect it to the
    // "destroy" signal to exit the GTK main loop when it is destroyed.
    win, err := gtk.WindowNew(gtk.WINDOW_TOPLEVEL)
    if err != nil {
        log.Fatal("Unable to create window:", err)
    }
    win.SetDecorated(false)
    win.SetTitle("Simple Example")
    win.Connect("destroy", func() {
        gtk.MainQuit()
    })

    // Create a new label widget to show in the window.
    l, err := gtk.LabelNew("Hello, gotk3!")
    if err != nil {
        log.Fatal("Unable to create label:", err)
    }

    ls, err := gtk.ListStoreNew(glib.TYPE_STRING, glib.TYPE_STRING)
    if err != nil {
        log.Fatal("Unable to create list:", err)
    }

    tv, err := gtk.TreeViewNewWithModel(ls)
    if err != nil {
        panic(err)
    }

    lb, err := gtk.ListBoxNew()
    if err != nil {
        panic(err)
    }

    fr, err := gtk.ListBoxRowNew()
    if err != nil {
        panic(err)
    }

    sr, err := gtk.ListBoxRowNew()
    if err != nil {
        panic(err)
    }

    tr, err := gtk.ListBoxRowNew()
    if err != nil {
        panic(err)
    }

    fr.Add(tv)
    tr.Add(l)

    fr.SetVExpand(true)
    tr.SetVExpand(false)
    lb.Add(fr)
    lb.Add(sr)
    lb.Add(tr)

    lb.SetVExpand(true)

    // Add the label to the window.
    win.Add(lb)

    // Set the default window size.
    win.SetDefaultSize(800, 600)

    // Recursively show all widgets contained in this window.
    win.ShowAll()

    // Begin executing the GTK main loop.  This blocks until
    // gtk.MainQuit() is run.
    gtk.Main()
}

所以我的问题是,是否有任何关于 GTK 如何进行布局以及如何让我想要的布局工作的综合文章。

标签: gogtk

解决方案


似乎这是不可能的ListBox。切换到Box并按预期工作。


推荐阅读