Gtk.ToolPalette
Gtk.ToolPalette工具选项板布局
继承关系
Gtk.ToolPalette是Gtk.Container的直接子类
Methods
方法修饰词 | 方法名及参数 |
---|---|
tatic | get_drag_target_group () |
static | get_drag_target_item () |
static | new () |
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
Name | Type | Flags | Short Description |
---|---|---|---|
icon-size | Gtk.IconSize | r/w/en | Size of icons in this tool palette |
icon-size-set | bool | r/w/en | Whether the icon-size property has been set |
toolbar-style | Gtk.ToolbarStyle | r/w/en | Style of items in the tool palette |
Signals
Name | Short 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()