【问题标题】:PyGObject: Drag and Drop with ToolPalettePyGObject:使用 ToolPalette 拖放
【发布时间】:2019-02-21 02:51:45
【问题描述】:

我已经能够从 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,
    )

有没有办法让拖放功能与 ToolPalette 一起工作,同时仍允许按钮正常工作?

【问题讨论】:

    标签: python drag-and-drop gtk gtk3 pygobject


    【解决方案1】:

    我的贡献者同事深入研究了 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-01
      • 1970-01-01
      • 2012-07-12
      • 1970-01-01
      • 2012-01-30
      相关资源
      最近更新 更多