Gtk.Table 和 Gtk.Grid 之间的差异

lf_*_*ujo 2 gtk genie

尽管 Gtk.table 已被弃用,但我用它得到了更好的结果,而不是推荐的 Gtk.Grid。

这可能是我的错误,但我找不到问题所在。

我的目标是创建一个 Gtk 窗口,顶部有一个笔记本,下面有两个按钮。这些按钮应水平对齐。

我的带有表格的代码按预期工作:

uses Gtk

class TestWindow : Window
    init
        // General characteristics of the window
        title = "Gtk Containers"
        default_height = 250
        default_width = 250
        window_position = WindowPosition.CENTER
        destroy.connect(Gtk.main_quit)

        // Now building the notebook
        var notebook = new Gtk.Notebook()
        var label1 = new Gtk.Label("Page one")
        var label2 = new Gtk.Label("Page two")
        var child1 = new Gtk.Label("Go to page 2 for the answer")
        var child2 = new Gtk.Label("Go to page 1 for the answer")
        notebook.append_page(child1, label1)
        notebook.append_page(child2, label2)

        // Now building the table
        var table = new Table(2,2,true)
        var button1 = new Gtk.Button.with_mnemonic("Button_1")
        var button2 = new Button.with_mnemonic("Button_2")

        // Attaching all elements into the table
        table.attach_defaults(notebook, 0,2,0,1)
        table.attach_defaults(button1, 0,1,1,2)
        table.attach_defaults(button2, 1,2,1,2)
        add(table)

init
    Gtk.init (ref args)
    var test = new TestWindow ()
    test.show_all ()
    Gtk.main ()
Run Code Online (Sandbox Code Playgroud)

然而,与推荐的 Gtk.Grid 相同的代码给了我两个没有笔记本的按钮:

uses Gtk

class TestWindow : Window
    init
        // General characteristics of the window
        title = "Gtk Containers"
        default_height = 250
        default_width = 250
        window_position = WindowPosition.CENTER
        destroy.connect(Gtk.main_quit)

        // Now building the notebook
        var notebook = new Gtk.Notebook()
        var label1 = new Gtk.Label("Page one")
        var label2 = new Gtk.Label("Page two")
        var child1 = new Gtk.Label("Go to page 2 for the answer")
        var child2 = new Gtk.Label("Go to page 1 for the answer")
        notebook.append_page(child1, label1)
        notebook.append_page(child2, label2)

        // Now building the grid
        var grid = new Grid()
        var button1 = new Gtk.Button.with_mnemonic("Button_1")
        var button2 = new Button.with_mnemonic("Button_2")

        // Attaching all elements into the grid
        grid.attach(notebook, 0,2,0,1)
        grid.attach(button1, 0,1,1,2)
        grid.attach(button2, 1,2,1,2)

init
    Gtk.init (ref args)
    var test = new TestWindow ()
    test.show_all ()
    Gtk.main ()
Run Code Online (Sandbox Code Playgroud)

如何使用网格而不是表格来达到目的?不要求代码,只要求指针。

and*_*abs 5

gtk_table_attach_defaults()gtk_grid_attach()以不同的方式进行操作。C语言的官方文档指出了这一点。

给定

        thing.attach(widget, a,b,c,d)
Run Code Online (Sandbox Code Playgroud)

对于 GtkTable,四个数字abcd是小部件给定边缘应占据的实际列号和行号。a是左边缘,b是右边缘,c是顶边缘,d是底边缘。

对于 GtkGrid,ab小部件左上角应占据的列, 是c行, 是小部件宽的列数,d是小部件高的行数。

或者换句话说,

        table.attach_defaults(widget, left, right, top, bottom)
Run Code Online (Sandbox Code Playgroud)

是相同的

        grid.attach(widget, left, top,
            right - left + 1, bottom - top + 1)
Run Code Online (Sandbox Code Playgroud)

希望该解释的某些部分能够澄清问题。

您的代码的快速修复是

        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)
Run Code Online (Sandbox Code Playgroud)