What do pickles and pythons have in common? Well, I'll get to that in a second. First, let's talk about application data. A key part of every program design is "how and where do I store my data". I have considered many different options for the OpenShot Video Editor, including XML, Embedded MySQL, and SqlLite.

The problem with using any of these methods, is that I will be responsible for writing all the data access code. That would include iterating through my complex object model, trying to persist all the various variables, objects, collections, and lists when the user clicks "save". Then when the user returns and "opens" their project, I would have to reverse the process. This can be a very tedious and buggy process. There has to be an easier way, right?

Here is where the pickles come into this story. There is a "pickle" module (as well as a faster cPickle module) that comes built in to Python. It is responsible for serializing and deserializing any Python object, including complex custom objects.

Here are some examples of how the cPickle module works:

Saving a custom object to the file system:
import cPickle as pickle
myFile = file(file_path, "wb")
pickle.dump(your_custom_object, myFile, True)

Loading a custom object from the file system:
import cPickle as pickle
myFile = file(file_path, "rb")
your_custom_object = pickle.load(myFile)


We've all heard the promise of GTK and how it will allow your application to be easily ported to other operating systems, such as Windows, Mac, Linux, and others. Something that you might not know is that if your GTK application uses the Gnome library, it will only work on Linux! This seems obvious to me now, but this really stumped me for a while.

Check it: I was using the Glade3 Interface designer to create some GTK forms, and I choose the Gnome App (instead of the GTK window). It looked the same, and was mostly assembled out of GTK widgets, but it has a subtle difference... it requires the Gnome library to be referenced. Long story short, I spend hours and hours trying to get my simple Gnome App to work on Windows with no luck. As soon as I replaced it with a standard GTK window, it worked great on Windows.

import gtk, gtk.glade # This is fine, and your program will work on all OSes
import gnome.ui #This will destroy your hopes of a cross-platform app

So, to wrap this up... be sure to use only 100% pure GTK if you are interested in a cross-platform application.


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.

import pygtk
import gtk

# function to track the motion of the cursor while dragging
def motion_cb(wid, context, x, y, time):
print "motion"
context.drag_status(gtk.gdk.ACTION_COPY, time)
return True

# function to print out the mime type of the drop item
def drop_cb(wid, context, x, y, time):
l.set_text('\n'.join([str(t) for t in context.targets]))
context.finish(True, False, time)
return True

# Create a GTK window and Label, and hook up
# drag n drop signal handlers to the window
w = gtk.Window()
w.set_size_request(200, 150)
w.drag_dest_set(0, [], 0)
w.connect('drag_motion', motion_cb)
w.connect('drag_drop', drop_cb)
w.connect('destroy', lambda w: gtk.main_quit())
l = gtk.Label()

# Start the program

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!

Subscribe to: Posts (Atom)