PyGobject(二十五)布局容器之Overlay

Gtk.Overlay

Gtk.Overlay覆盖布局,可以在一个部件上显示另一个部件

继承关系

Gtk.Overlay是Gtk.Bin的直接子类
这里写图片描述

Methods

方法修饰词方法名及参数
staticnew ()
add_overlay (widget)
get_overlay_pass_through (widget)
reorder_overlay (child, position)
set_overlay_pass_through (widget, pass_through)

Virtual Methods

do_get_child_position (widget, allocation)

Properties

NameTypeFlagsShort Description

Signals

NameShort Description
get-child-positionThe ::get-child-position signal is emitted to determine the position and size of any overlay child widgets.

例子

这里写图片描述
代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/12
# section 029
# 
# author: xiaosanyu
# website: yuxiaosan.tk \
#          http://blog.csdn.net/a87b01c14
# created: 16/7/12

TITLE = "Overlay"
DESCRIPTION = """
Shows widgets in static positions over a main widget.

The overlayed widgets can be interactive controls such
as the entry in this example, or just decorative, like
the big blue label.
"""

import gi

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


class OverlayWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="Overlay Example")
        self.set_size_request(500, 510)
        overlay = Gtk.Overlay()
        grid = Gtk.Grid()
        self.entry = Gtk.Entry()
        for i in range(25):
            button = Gtk.Button(str(i))
            button.set_size_request(100, 100)
            button.connect("clicked", self.on_click)
            grid.attach(button, i % 5, i / 5, 1, 1)
        overlay.add_overlay(grid)

        vbox = Gtk.VBox(spacing=10)
        overlay.add_overlay(vbox)
        overlay.set_overlay_pass_through(vbox, True)
        vbox.set_halign(Gtk.Align.CENTER)
        vbox.set_valign(Gtk.Align.CENTER)

        label = Gtk.Label.new("<span foreground='blue' weight='ultrabold' font='40'>Numbers</span>")
        label.set_use_markup(True)
        vbox.pack_start(label, False, False, 8)

        self.entry.set_placeholder_text("Your Lucky Number")
        vbox.pack_start(self.entry, False, False, 8)
        self.add(overlay)

    def on_click(self, button):
        self.entry.set_text(button.get_label())


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


if __name__ == "__main__":
    main()

代码解析

overlay = Gtk.Overlay()
grid = Gtk.Grid()

创建一个Overlay和一个Gtk.Grid

for i in range(25):
    Gtk.Button(str(i))
    button.set_size_request(100, 100)
    button.connect("clicked", self.on_click)
    grid.attach(button, i % 5, i / 5, 1, 1)

创建25个button,添加到grid中

overlay.add_overlay(grid)

将grid添加到overlay布局中

vbox = Gtk.VBox(spacing=10)
vbox.set_halign(Gtk.Align.CENTER)
vbox.set_valign(Gtk.Align.CENTER)
label = Gtk.Label.new("<span foreground='blue' weight='ultrabold' font='40'>Numbers</span>")
label.set_use_markup(True)
vbox.pack_start(label, False, False, 8)
self.entry.set_placeholder_text("Your Lucky Number")

创建一个垂直的Box,设置自己居中,向其中添加一个Label和一个Entry部件

 overlay.add_overlay(vbox)
 overlay.set_overlay_pass_through(vbox, True)

将这个box添加到overlay容器中。设置box可穿透。意思是被box覆盖的Button依然可以点击,如果设置为False,则被box覆盖的Button不可以点击。





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

余额充值