FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 14.18 | Next Entry >>

14.18. How do I get the clipboard functions to work properly?

First, get an instance of the default display from the global gtk.gdk.DisplayManager:

 display = gtk.gdk.display_manager_get().get_default_display()
Then get a reference to a gtk.Clipboard object, specifying the CLIPBOARD clipboard (and not PRIMARY):

 clipboard = gtk.Clipboard(display, "CLIPBOARD")
Now your cut/copy/paste callbacks can be written as follows:

 def on_cut_activate(self, obj):
 	textbuffer.cut_clipboard(clipboard, textview.get_editable())

 def on_copy_activate(self, obj):
 	textbuffer.copy_clipboard(clipboard)

 def on_paste_activate(self, obj):
 	textbuffer.paste_clipboard(clipboard, None, textview.get_editable())
Where "textview" is a reference to your gtk.TextView widget and "textbuffer" is a reference to your gtk.TextBuffer.

Note: the functions used in the callbacks are available as of PyGTK 2.4

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org