2017-08-08 29 views
1

我有一个网格包含框架内的一些标签,使它看起来像一个表。此网格插入垂直框中,其中直接的子标签正确居中(它们以与网格相同的方式包装在框中)。如何在一个垂直的Gtk.Box中水平居中一个Gtk.Grid?

我的简化代码如下所示:

import gi 
gi.require_version("Gtk", "3.0") 
from gi.repository import Gtk 

window = Gtk.Window() 

g = Gtk.Grid() # this should be in the horizontal center of the window 
g.attach(Gtk.Label("This should be centered but it is not."), 0, 0, 1, 1) 

b = Gtk.Box(orientation=Gtk.Orientation.VERTICAL) 
b.pack_start(g, True, False, 0) # The same behavior with: b.pack_start(g, True, True, 0) 
b.pack_start(Gtk.Label("This label is centered as it should be. Try to resize the window."), True, False, 0) 

window.add(b) 

window.connect("delete-event", Gtk.main_quit) 
window.show_all() 
Gtk.main() 

,这是它产生的GUI:

Screenshot of the code example.

回答

1

必须使用对齐和扩展属性/方法来设置子容器的行为。

使用Gtk.Box,您为两个孩子定义了展开为True并填充为False,但第一个是容器Gtk.Grid。将标签添加到网格时,您没有设置任何展开或填充标志。

不确定如何让标签在调整窗口大小时行为,但要让Gtk.Grid内部的标签水平居中,则可以将Gtk.Label水平展开设置为true或设置Gtk。网格对齐为居中。

实例 - 设置Gtk.Label水平扩大为真:

g = Gtk.Grid() # this should be in the horizontal center of the window 
l = Gtk.Label("This should be centered but it is not.") 
l.set_hexpand(True) 
g.attach(l, 0, 0, 1, 1) 

但是,如果垂直“成长”的窗口,标签将彼此分开。我建议你使用glade并且​​同时使用扩展和小部件对齐。

结果: result ui