PyGobject(四十二)布局容器之ButtonBox

Gtk.ButtonBox

Gtk.ButtonBox能够控制子部件的排列方式,如居中,靠左,靠右等。还能够控制子部件的主次关系,见例子。

继承关系

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

Methods

方法修饰词方法名及参数
staticnew (orientation)
get_child_non_homogeneous (child)
get_child_secondary (child)
get_layout ()
set_child_non_homogeneous (child, non_homogeneous)
set_child_secondary (child, is_secondary)
set_layout (layout_style)

Virtual Methods

Properties

NameTypeFlagsShort Description
layout-styleGtk.ButtonBoxStyler/w/enHow to lay out the buttons in the box. Possible values are: spread, edge, start and end

Signals

NameShort Description

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/28
# section 059
TITLE = "ButtonBox"
DESCRIPTION = ""
import gi

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


class ButtonBoxWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="ButtonBox Demo")
        self.set_border_width(10)
        main_vbox = Gtk.VBox()
        self.add(main_vbox)
        box = Gtk.ButtonBox(layout_style=Gtk.ButtonBoxStyle.EDGE)
        main_vbox.pack_start(box, False, False, 10)
        child1 = Gtk.Button(label="button1")
        child1.set_size_request(150, 30)
        child2 = Gtk.Button(label="button2")
        child3 = Gtk.Button(label="button3")
        box.add(child1)
        box.add(child2)
        box.add(child3)
        # set_child_non_homogeneous 设置False,其它孩子大小跟它同等,设置True,其他孩子尺寸跟它不一样
        box.set_child_non_homogeneous(child1, True)
        # set_child_secondary 设置孩子显示在次要位置.如果ButtonBox从左到右方向.则孩子显示在最右边.
        box.set_child_secondary(child2, True)

        frame_horz = Gtk.Frame.new("Horizontal Button Boxes")
        main_vbox.pack_start(frame_horz, True, True, 10)

        vbox = Gtk.VBox()
        vbox.set_border_width(10)
        frame_horz.add(vbox)
        vbox.pack_start(self.create_bbox(Gtk.Orientation.HORIZONTAL, "Spread", 40, Gtk.ButtonBoxStyle.SPREAD), True,
                        True, 0)
        vbox.pack_start(self.create_bbox(Gtk.Orientation.HORIZONTAL, "Edge", 40, Gtk.ButtonBoxStyle.EDGE), True, True,
                        5)
        vbox.pack_start(self.create_bbox(Gtk.Orientation.HORIZONTAL, "Start", 40, Gtk.ButtonBoxStyle.START), True, True,
                        5)
        vbox.pack_start(self.create_bbox(Gtk.Orientation.HORIZONTAL, "End", 40, Gtk.ButtonBoxStyle.END), True, True, 5)
        vbox.pack_start(self.create_bbox(Gtk.Orientation.HORIZONTAL, "Center", 40, Gtk.ButtonBoxStyle.CENTER), True,
                        True, 5)
        vbox.pack_start(self.create_bbox(Gtk.Orientation.HORIZONTAL, "Expand", 0, Gtk.ButtonBoxStyle.EXPAND), True,
                        True, 5)

        frame_vert = Gtk.Frame.new("Vertical Button Boxes")
        main_vbox.pack_start(frame_vert, True, True, 10)

        hbox = Gtk.HBox()
        hbox.set_border_width(10)
        frame_vert.add(hbox)

        hbox.pack_start(self.create_bbox(Gtk.Orientation.VERTICAL, "Spread", 10, Gtk.ButtonBoxStyle.SPREAD), True, True,
                        0)
        hbox.pack_start(self.create_bbox(Gtk.Orientation.VERTICAL, "Edge", 10, Gtk.ButtonBoxStyle.EDGE), True, True, 5)
        hbox.pack_start(self.create_bbox(Gtk.Orientation.VERTICAL, "Start", 10, Gtk.ButtonBoxStyle.START), True, True,
                        5)
        hbox.pack_start(self.create_bbox(Gtk.Orientation.VERTICAL, "End", 10, Gtk.ButtonBoxStyle.END), True, True, 5)
        hbox.pack_start(self.create_bbox(Gtk.Orientation.VERTICAL, "Center", 10, Gtk.ButtonBoxStyle.CENTER), True,
                        True, 5)
        hbox.pack_start(self.create_bbox(Gtk.Orientation.VERTICAL, "Expand", 0, Gtk.ButtonBoxStyle.EXPAND), True, True,
                        5)

    @staticmethod
    def create_bbox(horizontal, title, spacing, layout):
        frame = Gtk.Frame.new(title)
        bbox = Gtk.ButtonBox.new(orientation=horizontal)
        bbox.set_border_width(5)
        frame.add(bbox)

        bbox.set_layout(layout)
        bbox.set_spacing(spacing)

        button = Gtk.Button.new_with_label("OK")
        bbox.add(button)
        button = Gtk.Button.new_with_label("Cancel")
        bbox.add(button)
        button = Gtk.Button.new_with_label("Help")
        bbox.add(button)

        return frame


def main():
    win = ButtonBoxWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()


if __name__ == "__main__":
    main()

代码解析

child1 = Gtk.Button(label="button1")
child1.set_size_request(150, 30)
child2 = Gtk.Button(label="button2")
child3 = Gtk.Button(label="button3")
box.add(child1)
box.add(child2)
box.add(child3)

第一组的三个按钮组要讲解set_child_non_homogeneous方法和set_child_secondary

# set_child_non_homogeneous 设置False(默认值),其它孩子大小跟它同等,设置True,其他孩子尺寸跟它不一样
box.set_child_non_homogeneous(child1, True)
# set_child_secondary 设置孩子显示在次要位置.如果ButtonBox从左到右方向.则孩子显示在最右边.
box.set_child_secondary(child2, True)

接下来主要介绍 Gtk.ButtonBoxStyle的使用,代码结合示例图看,比较清楚明了,不啰嗦了~





代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

sanxiaochengyu

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

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

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

打赏作者

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

抵扣说明:

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

余额充值