PyGobject(四十五)布局容器之Grid

GtkGrid布局详解
本文介绍GtkGrid容器,一种用于组织子部件成行和列的布局方式。详细解释了其继承关系、方法、属性和信号等内容,并通过实例展示了如何使用Python进行GtkGrid布局。

Gtk.Grid

网格布局容器

继承关系

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

Methods

方法修饰词方法名及参数
staticnew ()
attach (child, left, top, width, height)
attach_next_to (child, sibling, side, width, height)
get_baseline_row ()
get_child_at (left, top)
get_column_homogeneous ()
get_column_spacing ()
get_row_baseline_position (row)
get_row_homogeneous ()
get_row_spacing ()
insert_column (position)
insert_next_to (sibling, side)
insert_row (position)
remove_column (position)
remove_row (position)
set_baseline_row (row)
set_column_homogeneous (homogeneous)
set_column_spacing (spacing)
set_row_baseline_position (row, pos)
set_row_homogeneous (homogeneous)
set_row_spacing (spacing)

attach(child, left, top, width, height)

Parameters:
    child (Gtk.Widget) –要添加的子部件
    left (int) – 子部件左侧部件的个数,left=0表示当前部件位于第一列,以此类推
    top (int) – 子部件上方的行数,top=0表示当前部件位于第一行,以此类推
    width (int) – 子部件横跨的列数
    height (int) – 子部件横跨的行数

attach_next_to (child, sibling, side, width, height)

Parameters:
    child (Gtk.Widget) –要添加的子部件
    sibling (Gtk.Widget) – 子部件要相邻的部件,如果为None,子部件要位于行开始或者结束的位置
    side (Gtk.PositionType) – 子部件与sibling部件的位置关系
    width (int) – 子部件横跨的列数
    height (int) – 子部件横跨的行数

Virtual Methods

Properties

NameTypeFlagsShort Description
baseline-rowintr/w/enThe row to align the to the baseline when valign is Gtk.Align.BASELINE
column-homogeneousboolr/w/en列宽是否相同
column-spacingintr/w/en列间距
row-homogeneousboolr/w/en行高是否相同
row-spacingintr/w/en行间距

Signals

NameShort Description

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/6/13
# section 062
TITLE = "Grid"
DESCRIPTION = """
Gtk.Grid is a container which arranges its child widgets in rows and columns.
"""
import gi

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


class GridWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Grid Example")
        grid = Gtk.Grid()
        self.add(grid)

        button1 = Gtk.Button(label="Button 1")
        button2 = Gtk.Button(label="Button 2")
        button3 = Gtk.Button(label="Button 3")
        button4 = Gtk.Button(label="Button 4")
        button5 = Gtk.Button(label="Button 5")
        button6 = Gtk.Button(label="Button 6")
        grid.add(button1)
        grid.attach(button2, 1, 0, 2, 1)
        grid.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)
        grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)
        grid.attach(button5, 1, 2, 1, 1)
        grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)


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


if __name__ == "__main__":
    main()

代码解析:

grid = Gtk.Grid()
self.add(grid)

创建一个Gtk.Grid,添加到当前窗口中
接着创建6个Gtk.Button

grid.add(button1)

使用Gtk.Container.add()方法添加button1到grid中

grid.attach(button2, 1, 0, 2, 1)

使用attach方法添加button2,位于第二列第一行,横跨两列一行

grid.attach_next_to(button3, button1, Gtk.PositionType.BOTTOM, 1, 2)

使用attach_next_to方法添加button3,位于button1的下方,横跨一列两行

grid.attach_next_to(button4, button3, Gtk.PositionType.RIGHT, 2, 1)

使用attach_next_to方法添加button4,位于button3的右方,横跨两列一行

grid.attach(button5, 1, 2, 1, 1)

使用attach方法添加button5,位于第二列第三行,占一列一行

grid.attach_next_to(button6, button5, Gtk.PositionType.RIGHT, 1, 1)

使用attach_next_to方法添加button6,位于button5的右方,占一列一行

附录

Gtk.PositionType

class Gtk.PositionType
Bases: GObject.GEnum

LEFT = 0

The feature is at the left edge.

RIGHT = 1

The feature is at the right edge.

TOP = 2

The feature is at the top edge.

BOTTOM = 3

The feature is at the bottom edge.





代码下载地址: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、付费专栏及课程。

余额充值