首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Gtk.Table和Gtk.Grid的差异

Gtk.Table和Gtk.Grid的差异
EN

Stack Overflow用户
提问于 2016-01-01 05:24:34
回答 3查看 2.2K关注 0票数 2

尽管不推荐Gtk.table,但我使用它来获得更好的结果,而不是推荐的Gtk.Grid。

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

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

我使用表的代码按预期工作:

代码语言:javascript
运行
复制
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相同的代码给了我两个没有笔记本的按钮:

代码语言:javascript
运行
复制
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 ()

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-01-01 06:22:02

gtk_table_attach_defaults()gtk_grid_attach()的操作方式不同。C格式的正式文件指出了这一点。

给定的

代码语言:javascript
运行
复制
        thing.attach(widget, a,b,c,d)

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

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

或者换句话说,

代码语言:javascript
运行
复制
        table.attach_defaults(widget, left, right, top, bottom)

是相同的

代码语言:javascript
运行
复制
        grid.attach(widget, left, top,
            right - left + 1, bottom - top + 1)

希望这个解释的某些部分能澄清一些事情。

您的代码的快速修复将是

代码语言:javascript
运行
复制
        grid.attach(notebook, 0,0,2,1)
        grid.attach(button1, 0,1,1,1)
        grid.attach(button2, 1,1,1,1)
票数 4
EN

Stack Overflow用户

发布于 2020-08-07 06:35:15

对于Gtk.Grid:

attach(child, left, top, width, height)

参数

  • 子(Gtk.Widget) -要添加的小部件
  • 左(int) -要将子节点的左侧附加到
  • top (int) -要将子节点的顶部附加到
  • 宽度(int) -子列的数量将跨越
  • 高度(int) -子行将跨越的行数

对于Gtk.Table:

attach(child, left_attach, right_attach, top_attach, bottom_attach, xoptions, yoptions, xpadding, ypadding)

参数

  • 子(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) -子部件上面和下面的填充量。

注意:列和行是从零索引的。

票数 2
EN

Stack Overflow用户

发布于 2017-04-02 15:34:01

实验室的解释是正确的,尽管他将1添加到grid.attach的右值和底部值的转换对我来说不是很好。由于我不得不转换其中的许多值,我编写了一个python脚本来简化这个过程,HTH:

代码语言:javascript
运行
复制
import sys

if len(sys.argv) < 2: raise Exception('Argument not provided')

args = ''.join([str(x) for x in sys.argv[1:]]).split(',')

if len(args) != 4: raise Exception('Please provide 4 attach arguments')

left, right, top, bottom = map(lambda a: int(a), args)
print("{}, {}, {}, {}".format(left, top, right - left, bottom - top))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34553769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档