Search code examples
pythonpygobjectgtk3

pygobject creating a drag and drop source


from gi.repository import Gtk, Gdk

def drag_data_get_cb(widget, drag_context, selection_data, info, time):
    print selection_data.get_data_type()
    print widget.get_text()
    return widget.get_text()

def drag_begin_cb(widget, dragcontext):
    print dragcontext, widget
    return dragcontext

label = Gtk.Label()
label.drag_source_set(Gdk.ModifierType.BUTTON1_MASK, [], Gdk.DragAction.COPY)
label.set_text("Drag Me!")
label.connect("drag_data_get", drag_data_get_cb)
label.connect("drag_begin", drag_begin_cb)

window = Gtk.Window()
window.add(label)
window.connect("delete_event", Gtk.main_quit)
window.set_default_size(300, 250)
window.show_all()

Gtk.main()

ive been hitting my head against a wall over this for a few days now, can anyone tell me why this doesnt allow me to drag text into other widgets? neither of the drag events fire at all


Solution

  • It says in this tutorial that you cannot use widgets without windows, such as Gtk.Label as drag and drop sources. You can replace the label with a button for instance:

    label = Gtk.Button.new_with_label("Drag Me!")
    

    in order for this example to work.