sil*_*gan 7 window grid-layout python-2.7 gtk3 msys2
我有一个 Grid ,其中包含 Frames 中的一些标签,使其看起来像一张桌子。这个网格被插入一个垂直的盒子中,其中直接子标签正确居中(它们以与网格相同的方式打包在盒子中)。
我的简化代码是这样的:
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()
Run Code Online (Sandbox Code Playgroud)
这是它生成的 GUI:
您必须使用 Alignment 和 Expand 属性/方法来设置子容器的行为。
使用 Gtk.Box,您将两个子节点的 expand 定义为 True,Fill 定义为 False,但第一个是容器 Gtk.Grid。当您将标签添加到网格时,您没有设置任何展开或填充标志。
不确定在调整窗口大小时您希望标签的行为方式,但要让 Gtk.Grid 内的标签水平居中,然后您可以将 Gtk.Label 水平扩展设置为 true 或将 Gtk.Grid Alignment 设置为居中.
示例 - 将 Gtk.Label 水平扩展设置为 True:
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)
Run Code Online (Sandbox Code Playgroud)
但是,如果您垂直“增长”窗口,标签将彼此分开。我建议您使用空地并同时使用扩展和小部件对齐方式。
| 归档时间: |
|
| 查看次数: |
2541 次 |
| 最近记录: |