PyGObject:使用ToolPalette拖放

问题描述 投票:0回答:1

我已经能够从Gtk.ToolPalette进行拖放工作,但仅限于设置Gtk.ToolButton.set_use_drag_window(True)时。但是,当单击ToolButton进行拖放时,它不会导致按钮实际被视觉上单击。我理解这是因为set_use_drag_window导致所有事件(甚至按钮点击)被拦截为拖动事件。

文档说使用Gtk.ToolPalette拖放的最简单方法是使用所需的拖动源调色板和所需的拖动目标小部件调用Gtk.ToolPalette.add_drag_dest()。基于GUI应用程序的复杂性,这与我需要的相反,因为我需要设置ToolPalette,然后在创建DrawingArea之后向拖动源添加回调。

我继承了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

在DrawingArea上,我将它作为一个拖动目标:

    view.drag_dest_set(
        Gtk.DestDefaults.MOTION,
        DiagramPage.VIEW_DND_TARGETS,
        Gdk.DragAction.MOVE | Gdk.DragAction.COPY | Gdk.DragAction.LINK,
    )

有没有办法让拖放工具使用工具调色板,同时仍然允许按钮正常工作?

python drag-and-drop gtk gtk3 pygobject
1个回答
0
投票

我的同事贡献者挖掘了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
© www.soinside.com 2019 - 2024. All rights reserved.