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

Gtk.ToolPalette

Gtk.ToolPalette工具选项板布局

继承关系

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

Methods

方法修饰词方法名及参数
taticget_drag_target_group ()
staticget_drag_target_item ()
staticnew ()
add_drag_dest (widget, flags, targets, actions)
get_drag_item (selection)
get_drop_group (x, y)
get_drop_item (x, y)
get_exclusive (group)
get_expand (group)
get_group_position (group)
get_hadjustment ()
get_icon_size ()
get_style ()
get_vadjustment ()
set_drag_source (targets)
set_exclusive (group, exclusive)
set_expand (group, expand)
set_group_position (group, position)
set_icon_size (icon_size)
set_style (style)
unset_icon_size ()
unset_style ()

Virtual Methods

Properties

NameTypeFlagsShort Description
icon-sizeGtk.IconSizer/w/enSize of icons in this tool palette
icon-size-setboolr/w/enWhether the icon-size property has been set
toolbar-styleGtk.ToolbarStyler/w/enStyle of items in the tool palette

Signals

NameShort Description

例子

这里写图片描述
代码:

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

TITLE = "ToolPalette"
DESCRIPTION = """
A Gtk.ToolPalette allows you to add Gtk.ToolItems to a palette-like
container with different categories and drag and drop support.
"""

import gi

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


class ToolPaletteWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self, title="ToolPalette Example")

        hbox = Gtk.HBox(spacing=6)
        self.palette = Gtk.ToolPalette()
        group1 = Gtk.ToolItemGroup(label="Test Category")
        self.palette.add(group1)
        item = Gtk.ToolButton()
        item.set_icon_name("document-open")
        group1.insert(item, -1)
        group2 = Gtk.ToolItemGroup(label="Edit")
        self.palette.add(group2)
        item = Gtk.ToolButton()
        item.set_icon_name("edit-cut")
        group2.insert(item, -1)
        item = Gtk.ToolButton()
        item.set_icon_name("edit-paste")
        group2.insert(item, -1)
        item = Gtk.ToolButton()
        item.set_icon_name("edit-copy")
        group2.insert(item, -1)
        hbox.add(self.palette)

        contents = Gtk.DrawingArea()
        contents.set_size_request(300, 300)
        hbox.add(contents)
        contents.set_app_paintable(True)

        contents.connect("drag-data-received", self.passive_canvas_drag_data_received)
        contents.connect("draw", self.on_draw)
        contents.connect("delete-event", Gtk.main_quit)

        self.palette.add_drag_dest(contents,
                                   Gtk.DestDefaults.ALL,
                                   Gtk.ToolPaletteDragTargets.ITEMS,
                                   Gdk.DragAction.COPY)
        self.add(hbox)

    def passive_canvas_drag_data_received(self, widget, drag_context, x, y, data, info, time):
        widget.item = self.palette.get_drag_item(data)
        widget.x = x
        widget.y = y
        widget.queue_draw()

    @staticmethod
    def on_draw(widget, cr):
        cr.set_source_rgb(1, 1, 1)
        cr.paint()
        if hasattr(widget, "item"):
            pixbuf = Gtk.IconTheme.get_default().load_icon(widget.item.get_icon_name(), 64, 0)
            Gdk.cairo_set_source_pixbuf(cr, pixbuf, widget.x, widget.y)
            cr.paint()


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


if __name__ == "__main__":
    main()

代码解析:

hbox = Gtk.HBox(spacing=6)

创建一个水平的HBox

self.palette = Gtk.ToolPalette()

创建一个Gtk.ToolPalette

group1 = Gtk.ToolItemGroup(label="Test Category")
self.palette.add(group1)

创建第一个Gtk.ToolItemGroup分组,将其添加到Gtk.ToolPalette中。

tem = Gtk.ToolButton()
item.set_icon_name("document-open")
group1.insert(item, -1)

创建一个Gtk.ToolButton,设置其图片,然后添加到分组中

同理创建第二组

group2 = Gtk.ToolItemGroup(label="Edit")

向其中添加了三个Gtk.ToolButton


contents = Gtk.DrawingArea()
contents.set_size_request(300, 300)
hbox.add(contents)
contents.set_app_paintable(True)

contents.connect("drag-data-received", self.passive_canvas_drag_data_received)
contents.connect("draw", self.on_draw)

接着创建了一个Gtk.DrawingArea,连接”drag-data-received”和”draw”信号

self.palette.add_drag_dest(contents,
                                   Gtk.DestDefaults.ALL,
                                   Gtk.ToolPaletteDragTargets.ITEMS,
                                   Gdk.DragAction.COPY)

设置ToolPalette的拖放目的地

 def passive_canvas_drag_data_received(self, widget, drag_context, x, y, data, info, time):
        widget.item = self.palette.get_drag_item(data)
        widget.x = x
        widget.y = y
        widget.queue_draw()

”drag-data-received“信号,捕捉到拖放目标,设置拖动的目标和当前的位置。然后请求DrawingArea进行重绘

def on_draw(widget, cr):
    cr.set_source_rgb(1, 1, 1)
    cr.paint()
    if hasattr(widget, "item"):
        pixbuf = Gtk.IconTheme.get_default().load_icon(widget.item.get_icon_name(), 64, 0)
        Gdk.cairo_set_source_pixbuf(cr, pixbuf, widget.x, widget.y)
        cr.paint()




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

余额充值