差异btw Gtk.Table和Gtk.Grid [英] Differences btw Gtk.Table and Gtk.Grid

查看:282
本文介绍了差异btw Gtk.Table和Gtk.Grid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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



我的代码与表一样,按预期工作:

 使用Gtk 

class TestWindow:Window
init
//窗口的一般特性
title =Gtk Containers
default_height = 250
default_width = 250
window_position = WindowPosition.CENTER
destroy.connect(Gtk.main_quit)

//现在构建笔记本
var notebook = new Gtk.Notebook()
var label1 = new Gtk.Label(Page one)
var label2 = new Gtk.Label(Page two)
var child1 = new Gtk .Label(转到第2页的答案)
var child2 = new Gtk.Label(转到第1页的答案)
notebook.append_page(child1,label1)
notebook.append_page(child2,label2)

//现在构建表
var table = new Table(2,2,true)
var button 1 = new Gtk.Button.with_mnemonic(Button_1)
var button2 = new Button.with_mnemonic(Button_2)

//将所有元素附加到表中
表.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使用相同的代码给了我两个没有笔记本的按钮:

 使用Gtk 

class TestWindow:Window
init
//窗口的一般特性
title =Gtk Containers
default_height = 250
default_width = 250
window_position = WindowPosition.CENTER
destroy.connect( Gtk.main_quit)

//现在构建n otebook
var notebook = new Gtk.Notebook()
var label1 = new Gtk.Label(Page one)
var label2 = new Gtk.Label(Page two)
var child1 = new Gtk.Label(转到第2页的答案)
var child2 = new Gtk.Label(转到第1页的答案)
notebook.append_page( child1,label1)
notebook.append_page(child2,label2)

//现在构建网格
var grid = new Grid()
var button1 = new Gtk .Button.with_mnemonic(Button_1)
var button2 = new Button.with_mnemonic(Button_2)

//将所有元素附加到网格中
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 i实现目标而不是表格?

解决方案

gtk_table_attach_defaults() gtk_grid_attach()的操作方式不同。 C中的官方文档指出了这一点。

给定

  thing.attach(widget,a,b, c,d)

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



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



或换句话说,

  table.attach_defaults(小工具,左,右,上,下)



  grid.attach(窗口小部件,左,顶部,
右 - 左+ 1,底部 - 顶部+ 1)

希望这个解释的某些部分能够清除一切。



代码的快速修复将是

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


Althought Gtk.table is deprecated, I am getting better results with it, instead of the recommended Gtk.Grid.

It is probably my mistake, but I couldn't find the problem.

My aim is to create a Gtk window with a notebook at the top and two buttons below. These buttons should be horizontally aligned.

My code with table, works as expected:

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 ()

However, the same code with the recommended Gtk.Grid gives me the two buttons without the notebook:

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 ()

How to achieve the aim using Grid instead of Tables? Not asking for code, just a pointer.

解决方案

gtk_table_attach_defaults() and gtk_grid_attach() operate differently. The official documentation in C points this out.

Given

        thing.attach(widget, a,b,c,d)

For GtkTable, the four numbers a, b, c, and d are the actual column and row numbers that the given edge of the widget should occupy. a is the left edge, b is the right edge, c is the top edge, and d is the bottom edge.

For GtkGrid, a is the column and b is the row that the top-left corner of the widget should occupy, c is the number of columns wide the widget is, and d is the number of rows tall the widget is.

Or in other words,

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

is the same as

        grid.attach(widget, left, top,
            right - left + 1, bottom - top + 1)

Hopefully some part of that explanation clears things up.

The quick fix for your code would be

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

这篇关于差异btw Gtk.Table和Gtk.Grid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆