Once again I find myself struggling with Drag 'n Drop using GTK and Python. Why is it so difficult to get something working? Why do all the examples on the Internet suck? These are all good questions my friends, and today I bring some answers.

First of all, there are many different levels of drag 'n drop in GTK. There are some higher level methods and some lower level methods. I won't pretend to be an expert, but instead I will share a SUPER USEFUL example I've found and modified. If you are struggling to implement drag 'n drop in your application, please stop now, and run this example. Hopefully, this will work with your own widgets and save you some time.

importpygtkpygtk.require('2.0')importgtk

# function to track the motion of the cursor while draggingdefmotion_cb(wid,context,x,y,time):print"motion"context.drag_status(gtk.gdk.ACTION_COPY,time)returnTrue

# function to print out the mime type of the drop itemdefdrop_cb(wid,context,x,y,time):l.set_text('\n'.join([str(t)fortincontext.targets]))context.finish(True,False,time)returnTrue

My previous posts about drag n' drop used some of the higher level methods and signals, such as drag_data_received. This worked great for dropping text/uri-list type items on a gtkTreeView, but it didn't work for tree nodes (which are type GTK_TREE_MODEL_ROW). In other words, it didn't work with a tree node as a drop item... instead it seemed to ignore that type of item.

So, to wrap this up, use the above example in your program, cross your fingers, and I hope this might save you some time. Good luck!

Popular entries

Publicações Mais Recentes

I am proud to announce the immediate release of OpenShot 2.4.4, the absolute best version yet! This is going to be a long post, but here is a quick summary for those who are short on time. This releas...

I am very proud to announce the release of OpenShot 2.4.3, our latest and greatest version! A huge thanks to the community for contributing bug reports, enhancements, translations, and fixes!
Download...

Happy summer! Version 2.4.2, the latest and greatest OpenShot Video Editor is now available (after a long wait), and we have lots of great improvements to share! We have exciting new effects, tons of ...