Gtk表格和Gtk表格的区别

Gtk表格和Gtk表格的区别,gtk,genie,Gtk,Genie,虽然Gtk.table已被弃用,但我使用它获得了更好的结果,而不是推荐的Gtk.Grid 这可能是我的错误,但我找不到问题所在 我的目标是创建一个Gtk窗口,顶部有一个笔记本,下面有两个按钮。这些按钮应水平对齐 我与表的代码按预期工作: uses Gtk class TestWindow : Window init // General characteristics of the window title = "Gtk Containers"

虽然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 ()
但是,与推荐的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 ()

如何使用网格而不是表格来实现目标?不需要代码,只需要一个指针。

gtk\u table\u attach\u defaults()
gtk\u grid\u attach()
的操作方式不同。报告指出了这一点

给定

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

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

换句话说

        table.attach_defaults(widget, left, right, top, bottom)

        grid.attach(widget, left, top,
            right - left + 1, bottom - top + 1)
希望这个解释的某些部分能澄清问题

代码的快速修复方法是

        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)
的解释是正确的,尽管他的转换将1添加到grid.attach right和bottom值对我来说不太合适。由于我必须转换其中许多值,因此我准备了一个python脚本来简化此过程,HTH:

导入系统 如果len(sys.argv)<2:引发异常('未提供参数') args=''.join([str(x)代表sys.argv[1:]]中的x)).split(',')) 如果len(args)!=4:引发异常('请提供4个附加参数') 左、右、上、下=映射(lambda:int(a),args) 打印(“{},{},{},{}.”格式(左、上、右-左、下-上)) 对于Gtk.Grid:

附加(儿童、左侧、顶部、宽度、高度)

参数

  • child(Gtk.Widget)–要添加的小部件
  • left(int)–要将子对象左侧附加到的列号
  • top(int)–要将子对象的顶部附加到的行号
  • 宽度(int)–子级将跨越的列数
  • 高度(int)–子级将跨越的行数
对于Gtk.表:

attach(子、左、右、上、下、xoptions、yooptions、xpadding、ypadding)

参数

  • child(Gtk.Widget)–要添加的小部件
  • left_attach(int)–要将子小部件左侧附加到的列号
  • right_attach(int)–要将子小部件右侧附加到的列号
  • top_attach(int)–要将子小部件顶部附加到的行号
  • bottom_attach(int)–要将子小部件底部附加到的行号
  • xoptions(Gtk.AttachOptions)–用于在调整表的大小时指定子小部件的属性
  • yoptions(Gtk.AttachOptions)–与xoptions相同,只是此字段决定垂直调整大小的行为
  • xpadding(int)–一个整数值,指定要添加到表中的小部件左右两侧的填充
  • ypadding(int)–子窗口小部件上方和下方的填充量
注意:列和行的索引从零开始

        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)