21. Drag and Drop — Python GTK+ 3 Tutorial 3.4 documentation (original) (raw)

Note

Versions of PyGObject < 3.0.3 contain a bug which does not allow drag and drop to function correctly. Therefore a version of PyGObject >= 3.0.3 is required for the following examples to work.

Setting up drag and drop between widgets consists of selecting a drag source (the widget which the user starts the drag from) with theGtk.Widget.drag_source_set() method, selecting a drag destination (the widget which the user drops onto) with the Gtk.Widget.drag_dest_set()method and then handling the relevant signals on both widgets.

Instead of using Gtk.Widget.drag_source_set() andGtk.Widget.drag_dest_set() some specialised widgets require the use of specific functions (such as Gtk.TreeView and Gtk.IconView).

A basic drag and drop only requires the source to connect to the “drag-data-get” signal and the destination to connect to the “drag-data-received” signal. More complex things such as specific drop areas and custom drag icons will require you to connect to additional signals and interact with the Gdk.DragContext object it supplies.

In order to transfer data between the source and destination, you must interact with the Gtk.SelectionData variable supplied in the“drag-data-get” and “drag-data-received”signals using the Gtk.SelectionData get and set methods.

21.1. Target Entries

To allow the drag source and destination to know what data they are receiving and sending, a common list of Gtk.TargetEntry's are required. A Gtk.TargetEntry describes a piece of data that will be sent by the drag source and received by the drag destination.

There are two ways of adding Gtk.TargetEntry's to a source and destination. If the drag and drop is simple and each target entry is of a different type, you can use the group of methods mentioned here.

If you require more than one type of data or wish to do more complex things with the data, you will need to create the Gtk.TargetEntry'susing the Gtk.TargetEntry.new() method.

21.2. Drag Source Signals

Name When it is emitted Common Purpose
drag-begin User starts a drag Set-up drag icon
drag-data-get When drag data is requested by the destination Transfer drag data from source to destination
drag-data-delete When a drag with the action Gdk.DragAction.MOVE is completed Delete data from the source to complete the ‘move’
drag-end When the drag is complete Undo anything done in drag-begin

21.3. Drag Destination Signals

Name When it is emitted Common Purpose
drag-motion Drag icon moves over a drop area Allow only certain areas to be dropped onto
drag-drop Icon is dropped onto a drag area Allow only certain areas to be dropped onto
drag-data-received When drag data is received by the destination Transfer drag data from source to destination

21.4. Example

_images/drag_and_drop_example.png

1import gi 2 3gi.require_version("Gtk", "3.0") 4from gi.repository import Gtk, Gdk, GdkPixbuf 5 6(TARGET_ENTRY_TEXT, TARGET_ENTRY_PIXBUF) = range(2) 7(COLUMN_TEXT, COLUMN_PIXBUF) = range(2) 8 9DRAG_ACTION = Gdk.DragAction.COPY 10 11 12class DragDropWindow(Gtk.Window): 13 def init(self): 14 super().init(title="Drag and Drop Demo") 15 16 vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=6) 17 self.add(vbox) 18 19 hbox = Gtk.Box(spacing=12) 20 vbox.pack_start(hbox, True, True, 0) 21 22 self.iconview = DragSourceIconView() 23 self.drop_area = DropArea() 24 25 hbox.pack_start(self.iconview, True, True, 0) 26 hbox.pack_start(self.drop_area, True, True, 0) 27 28 button_box = Gtk.Box(spacing=6) 29 vbox.pack_start(button_box, True, False, 0) 30 31 image_button = Gtk.RadioButton.new_with_label_from_widget(None, "Images") 32 image_button.connect("toggled", self.add_image_targets) 33 button_box.pack_start(image_button, True, False, 0) 34 35 text_button = Gtk.RadioButton.new_with_label_from_widget(image_button, "Text") 36 text_button.connect("toggled", self.add_text_targets) 37 button_box.pack_start(text_button, True, False, 0) 38 39 self.add_image_targets() 40 41 def add_image_targets(self, button=None): 42 targets = Gtk.TargetList.new([]) 43 targets.add_image_targets(TARGET_ENTRY_PIXBUF, True) 44 45 self.drop_area.drag_dest_set_target_list(targets) 46 self.iconview.drag_source_set_target_list(targets) 47 48 def add_text_targets(self, button=None): 49 self.drop_area.drag_dest_set_target_list(None) 50 self.iconview.drag_source_set_target_list(None) 51 52 self.drop_area.drag_dest_add_text_targets() 53 self.iconview.drag_source_add_text_targets() 54 55 56class DragSourceIconView(Gtk.IconView): 57 def init(self): 58 Gtk.IconView.init(self) 59 self.set_text_column(COLUMN_TEXT) 60 self.set_pixbuf_column(COLUMN_PIXBUF) 61 62 model = Gtk.ListStore(str, GdkPixbuf.Pixbuf) 63 self.set_model(model) 64 self.add_item("Item 1", "image-missing") 65 self.add_item("Item 2", "help-about") 66 self.add_item("Item 3", "edit-copy") 67 68 self.enable_model_drag_source(Gdk.ModifierType.BUTTON1_MASK, [], DRAG_ACTION) 69 self.connect("drag-data-get", self.on_drag_data_get) 70 71 def on_drag_data_get(self, widget, drag_context, data, info, time): 72 selected_path = self.get_selected_items()[0] 73 selected_iter = self.get_model().get_iter(selected_path) 74 75 if info == TARGET_ENTRY_TEXT: 76 text = self.get_model().get_value(selected_iter, COLUMN_TEXT) 77 data.set_text(text, -1) 78 elif info == TARGET_ENTRY_PIXBUF: 79 pixbuf = self.get_model().get_value(selected_iter, COLUMN_PIXBUF) 80 data.set_pixbuf(pixbuf) 81 82 def add_item(self, text, icon_name): 83 pixbuf = Gtk.IconTheme.get_default().load_icon(icon_name, 16, 0) 84 self.get_model().append([text, pixbuf]) 85 86 87class DropArea(Gtk.Label): 88 def init(self): 89 Gtk.Label.init(self) 90 self.set_label("Drop something on me!") 91 self.drag_dest_set(Gtk.DestDefaults.ALL, [], DRAG_ACTION) 92 93 self.connect("drag-data-received", self.on_drag_data_received) 94 95 def on_drag_data_received(self, widget, drag_context, x, y, data, info, time): 96 if info == TARGET_ENTRY_TEXT: 97 text = data.get_text() 98 print("Received text: %s" % text) 99 100 elif info == TARGET_ENTRY_PIXBUF: 101 pixbuf = data.get_pixbuf() 102 width = pixbuf.get_width() 103 height = pixbuf.get_height() 104 105 print("Received pixbuf with width %spx and height %spx" % (width, height)) 106 107 108win = DragDropWindow() 109win.connect("destroy", Gtk.main_quit) 110win.show_all() 111Gtk.main()