Gtk.Table 和 Gtk.Grid 之间的区别
Differences btw Gtk.Table and Gtk.Grid
虽然 Gtk.table 已被弃用,但我用它获得了更好的结果,而不是推荐的 Gtk.Grid。
可能是我的错误,但我找不到问题。
我的目标是创建一个 Gtk window,顶部有一个笔记本,下面有两个按钮。这些按钮应水平对齐。
我的 table 代码按预期工作:
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 ()
如何用Grid代替Tables达到目的?不要求代码,只是一个指针。
gtk_table_attach_defaults()
和 gtk_grid_attach()
的操作不同。 official documentation in C 指出了这一点。
给定
thing.attach(widget, a,b,c,d)
对于 GtkTable,四个数字 a
、b
、c
和 d
是小部件的给定边缘应该的实际列号和行号占据。 a
是左边,b
是右边,c
是上边,d
是下边。
对于GtkGrid,a
是列,b
是widget的top-left角应该占据的行,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)
andlabs的解释是正确的,虽然他的转换加1到grid.attach右边和底部值对我来说效果不佳。由于我必须转换许多这些值,我准备了一个 python 脚本来简化这个过程,HTH:
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))
对于Gtk.Grid:
attach(child, left, top, width, height)
参数:
- child (Gtk.Widget) – 要添加的小部件
- left (int) – 将 child 的左侧附加到
的列号
- top (int) – 将 child 的顶部附加到
的行号
- width (int) – child 将跨越的列数
- height (int) – child 将跨越的行数
对于Gtk.Table:
attach(child, left_attach, right_attach, top_attach, bottom_attach, xoptions, yoptions, xpadding, ypadding)
参数:
- child (Gtk.Widget) – 要添加的小部件。
- left_attach (int) – 要将 child 小部件的左侧附加到的列号。
- right_attach (int) – child 小部件右侧附加到的列号。
- top_attach (int) – child 小部件顶部附加到的行号。
- bottom_attach (int) – 附加 child 小部件底部的行号。
- xoptions (Gtk.AttachOptions) – 用于在 table 调整大小时指定 child 小部件的属性。
- yoptions (Gtk.AttachOptions) – 与 xoptions 相同,除了此字段确定垂直调整大小的行为。
- xpadding (int) – 一个整数值,指定要添加到 table.
的小部件左侧和右侧的填充
- ypadding (int) – child 小部件上方和下方的填充量。
注意:列和行是从零开始索引的。
虽然 Gtk.table 已被弃用,但我用它获得了更好的结果,而不是推荐的 Gtk.Grid。
可能是我的错误,但我找不到问题。
我的目标是创建一个 Gtk window,顶部有一个笔记本,下面有两个按钮。这些按钮应水平对齐。
我的 table 代码按预期工作:
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 ()
如何用Grid代替Tables达到目的?不要求代码,只是一个指针。
gtk_table_attach_defaults()
和 gtk_grid_attach()
的操作不同。 official documentation in C 指出了这一点。
给定
thing.attach(widget, a,b,c,d)
对于 GtkTable,四个数字 a
、b
、c
和 d
是小部件的给定边缘应该的实际列号和行号占据。 a
是左边,b
是右边,c
是上边,d
是下边。
对于GtkGrid,a
是列,b
是widget的top-left角应该占据的行,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)
andlabs的解释是正确的,虽然他的转换加1到grid.attach右边和底部值对我来说效果不佳。由于我必须转换许多这些值,我准备了一个 python 脚本来简化这个过程,HTH:
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))
对于Gtk.Grid:
attach(child, left, top, width, height)
参数:
- child (Gtk.Widget) – 要添加的小部件
- left (int) – 将 child 的左侧附加到 的列号
- top (int) – 将 child 的顶部附加到 的行号
- width (int) – child 将跨越的列数
- height (int) – child 将跨越的行数
对于Gtk.Table:
attach(child, left_attach, right_attach, top_attach, bottom_attach, xoptions, yoptions, xpadding, ypadding)
参数:
- child (Gtk.Widget) – 要添加的小部件。
- left_attach (int) – 要将 child 小部件的左侧附加到的列号。
- right_attach (int) – child 小部件右侧附加到的列号。
- top_attach (int) – child 小部件顶部附加到的行号。
- bottom_attach (int) – 附加 child 小部件底部的行号。
- xoptions (Gtk.AttachOptions) – 用于在 table 调整大小时指定 child 小部件的属性。
- yoptions (Gtk.AttachOptions) – 与 xoptions 相同,除了此字段确定垂直调整大小的行为。
- xpadding (int) – 一个整数值,指定要添加到 table. 的小部件左侧和右侧的填充
- ypadding (int) – child 小部件上方和下方的填充量。
注意:列和行是从零开始索引的。