我已经能够从gtk.toolpalette上工作,但仅在设置 Gtk.ToolButton.set_use_drag_window(True)
时才能工作。但是,当单击工具按钮拖放它时,它实际上不会导致按钮实际单击。我知道这是因为set_use_drag_window
导致所有事件(甚至按钮点击)被拦截为拖动事件。
文档说,使用gtk.toolpalette使用拖放的最简单方法是用所需的拖放源调色板和所需的拖动目标窗口小部件调用Gtk.ToolPalette.add_drag_dest()
。这与基于GUI应用的复杂性所需的相反,因为我需要设置工具板,然后在创建DrawchArea时将回调添加到拖放源中。
我已经从Gtk.TooPalette
继承,为调色板的每个部分创建了一个Gtk.ToolItemGroup
,然后我正在创建按钮:
def toolbox_button(self, action_name, stock_id):
button = Gtk.ToolButton.new_from_stock(stock_id)
button.action_name = action_name
button.set_use_drag_window(True)
# Enable Drag and Drop
button.drag_source_set(
Gdk.ModifierType.BUTTON1_MASK,
self.DND_TARGETS,
Gdk.DragAction.COPY | Gdk.DragAction.LINK,
)
button.drag_source_set_icon_stock(stock_id)
button.connect("drag-data-get", self._button_drag_data_get)
return button
在绘图架上,我正在使其成为一个阻力dest:
view.drag_dest_set(
Gtk.DestDefaults.MOTION,
DiagramPage.VIEW_DND_TARGETS,
Gdk.DragAction.MOVE | Gdk.DragAction.COPY | Gdk.DragAction.LINK,
)
是否有一种方法可以拖放到工具计划,同时仍允许按钮正常工作?
我的其他贡献者挖掘到GTK源代码中,并弄清楚GTK.ToggleToolbutton实际上有一个儿童按钮,目前尚未记录。如果将拖放源设置为此"内部按钮",则拖放工作可行。
def toolbox_button(action_name, stock_id, label, shortcut):
button = Gtk.ToggleToolButton.new()
button.set_icon_name(stock_id)
button.action_name = action_name
if label:
button.set_tooltip_text("%s (%s)" % (label, shortcut))
# Enable Drag and Drop
inner_button = button.get_children()[0]
inner_button.drag_source_set(
Gdk.ModifierType.BUTTON1_MASK | Gdk.ModifierType.BUTTON3_MASK,
self.DND_TARGETS,
Gdk.DragAction.COPY | Gdk.DragAction.LINK,
)
inner_button.drag_source_set_icon_stock(stock_id)
inner_button.connect(
"drag-data-get", self._button_drag_data_get, action_name
)
return button