您的位置:首页 > 移动开发 > Objective-C

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

2016-07-30 10:49 387 查看

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息