PyGTK Drag and Drop - PyGTK

What is PyGTK Drag and Drop?

Widgets having related X Window are accomplished of drag and drop. In the program, a widget as a source and/or destination for drag-and-drop must first be chosen. The widget defined as source can send out the dragged data. The destination widget accepts it when dragged data is dropped on it.

The resulting steps are complicated in setting up a drag-and-drop enabled application −

Step 1 − Setting up a source widget.
Step 2 − The drag_source_set() method specifies the target types for a drag operation –

Step 3 − The start_button_mask argument specifies a bitmask of buttons that starts the drag operation.
Step 4 − The target argument is a list of tuples of this structure –

The target argument is a string representing drag type, for instance, text/plain or image/x-xpixmap.

Step 6 − The resulting flags are predefined −


Step 7 − There will be no limitation as the flag is set to 0.

If the widget is not required to act as source, it can be unset −


The source signal emits signals. The following table lists the signals and their callbacks.


def drag_begin_cb(widget, drag_context, data):


def drag_data_get_cb(widget, drag_context, selection_data, info, time, data):


def drag_data_delete_cb(widget, drag_context, data):


def drag_end_cb(widget, drag_context, data):

Setting up a Destination Widget

The drag_dest_set() method specifies which widget can receive dragged data.

The flags parameter can take one of the following constants −


This checks if the drag matches this widget's list of possible targets and actions, then calls the drag_status() as appropriate.


This draws a highlight on this widget as long as a drag is over this widget


When a drop occurs, if the drag matches this widget's list of possible targets and actions calldrag_get_data()on behalf of the widget. Whether or not the drop is successful, calldrag_finish(). If the action was a move and the drag was successful, then TRUE will be passed for the delete parameter todrag_finish().


If set, specifies that all default actions should be taken.

The target is a list of tuples covering target information. The actions argument is a bitmask of or a combination of one or more of the resulting values −

  • gtk.gdk.ACTION_DEFAULT
  • gtk.gdk.ACTION_COPY
  • gtk.gdk.ACTION_MOVE
  • gtk.gdk.ACTION_LINK
  • gtk.gdk.ACTION_PRIVATE
  • gtk.gdk.ACTION_ASK

The "drag-motion" trainer must control if the drag data is suitable by matching the destination targets with the gtk.gdk.DragContext targets and optionally by investigative the drag data by calling the drag_get_data()method. The gtk.gdk.DragContext. drag_status() method must be called to update the drag_context status.

The "drag-drop" handler must control the corresponding target using the drag_dest_find_target() method and then ask for the drag data using the drag_get_data() method. The data will be obtainable in the "drag-data-received" handler.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PyGTK Topics