PyGobject(四十一)布局容器之Box

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Gtk.Box

在前面的文章中,所讲的布局容器全部是Gtk.Bin的子类,就是说都是单孩子容器。接下来的篇幅将会介绍多孩子容器。它们都是Gtk.Container的子类,单不是Gtk.Bin的子类

Gtk.Box盒子布局,允许孩子从左到右或者从上到下的排布。
它有两个子类,Gtk.HBox(水平布局)和Gtk.VBox(垂直布局)

继承关系

Gtk.Box是Gtk.Container的直接子类
这里写图片描述

Methods

方法修饰词方法名及参数
staticnew (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

NameTypeFlagsShort Description
baseline-positionGtk.BaselinePositionr/w/en基线位置
homogeneousboolr/w/en子部件是否拥有同样的大小
spacingintr/w/en子部件之间的间距

Signals

NameShort 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值