Gtk.Box
在前面的文章中,所讲的布局容器全部是Gtk.Bin的子类,就是说都是单孩子容器。接下来的篇幅将会介绍多孩子容器。它们都是Gtk.Container的子类,单不是Gtk.Bin的子类
Gtk.Box盒子布局,允许孩子从左到右或者从上到下的排布。
它有两个子类,Gtk.HBox(水平布局)和Gtk.VBox(垂直布局)
继承关系
Gtk.Box是Gtk.Container的直接子类
Methods
| 方法修饰词 | 方法名及参数 |
|---|---|
| static | new (orientation, spacing) |
| get_baseline_position () | |
| get_center_widget () | |
| get_homogeneous () | |
| get_spacing () | |
| pack_end (child, expand, fill, padding) | |
| pack_start (child, expand, fill, padding) | |
| query_child_packing (child) | |
| reorder_child (child, position) | |
| set_baseline_position (position) | |
| set_center_widget (widget) | |
| set_child_packing (child, expand, fill, padding, pack_type) | |
| set_homogeneous (homogeneous) | |
| set_spacing (spacing) |
Virtual Methods
Properties
| Name | Type | Flags | Short Description |
|---|---|---|---|
| baseline-position | Gtk.BaselinePosition | r/w/en | 基线位置 |
| homogeneous | bool | r/w/en | 子部件是否拥有同样的大小 |
| spacing | int | r/w/en | 子部件之间的间距 |
Signals
| Name | Short Description |
|---|
例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/13
# section 058
TITLE = "Box"
DESCRIPTION = """
The Gtk.Box widget organizes child widgets into a rectangular area
"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
class BoxWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Box Example")
self.box = Gtk.Box(spacing=6)
# self.box = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
self.add(self.box)
self.button1 = Gtk.Button(label="Hello")
self.button1.connect("clicked", self.on_button_clicked)
self.box.pack_start(self.button1, True, True, 0)
self.button2 = Gtk.Button(label="Goodbye")
self.button2.connect("clicked", self.on_button_clicked)
self.box.pack_start(self.button2, True, True, 0)
@staticmethod
def on_button_clicked(widget):
print(widget.get_label())
def main():
win = BoxWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
if __name__ == "__main__":
main()
代码解析
self.box = Gtk.Box(spacing=6)
创建一个水平(默认)Box,子部件间的间距为6
像其中添加两个Gtk.Button
如果要创建垂直布局的Box,使用
self.box = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL)
或者
self.box=Gtk.Box(spacing=6)
self.box.set_orientation(Gtk.Orientation.VERTICAL)
或者
self.box =Gtk.VBox(spacing=6)
代码下载地址:http://download.youkuaiyun.com/detail/a87b01c14/9594728

本文介绍了Gtk.Box布局容器的使用方法,包括如何创建水平和垂直布局的Box,以及如何设置子部件间的间距等属性。提供了Python代码示例,展示了如何通过Gtk.Box组织子部件。
697

被折叠的 条评论
为什么被折叠?



