PyGTK - Clipboard Class



A Clipboard object holds shared data between two processes or two widgets of the same application. The gtk.Clipboard is a high level interface for the gtk.SelectionData class.

The following is a prototype of the gtk.Clipboard constructor −

gtk.Clipboard(display,selction)

Here, the display parameter corresponds to the gtk.gdk.Display object for which the clipboard is to be created or retrieved. By default, it is the standard output device. The selection parameter defaults to CLIPBOARD, an object representing an interned string.

PyGTK provides a convenience function to create a clipboard object with defaults.

gtk.clipboard.get()

gtk.Clipboard class has the following methods −

  • Clipboard.store() − This stores the current clipboard data somewhere so that it will stay around even after the application has quit.

  • Clipboard.clear() − This removes the contents of the clipboard.

  • Clipboard.set_text(text) − This sets the contents of the clipboard to the string.

  • Clipboard.request_text() − This requests the contents of the clipboard as text. When the text is later received, callback will be called with the data specified by user_data. The signature of callback is:

    • def callback(clipboard, text, data) − text will contain the text retrieved from clipboard.

As a demonstration of clipboard, the following code uses two TextViews and two buttons on a toplevel gtk.Window. The 'Set' button calls the on_set() function which puts the text from first textView on the clipboard.

buf = self.tv1.get_buffer()
text = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
self.clipboard = gtk.clipboard_get()
self.clipboard.set_text(text)
self.clipboard.store()

When the second button ('retrieved') is pressed, the data from clipboard is fetched by the request_text() method −

self.clipboard.request_text(self.readclipboard,        user_data = None)

The content of user_data goes to a callback method readclipboard() which displays it on second textview.

def readclipboard(self, clipboard, text, data):
   buffer = gtk.TextBuffer()
   buffer.set_text(text)
   self.tv2.set_buffer(buffer)

Example

The following is the entire code for clipboard operation −

import gtk

class PyApp(gtk.Window):
   
   def __init__(self):
      super(PyApp, self).__init__()
      
	  self.set_title("Clipboard demo")
      self.set_size_request(300,200)
      self.set_position(gtk.WIN_POS_CENTER)
		
      vbox = gtk.VBox(False, 5)
      self.tv1 = gtk.TextView()
		
      vbox.add(self.tv1)
      self.tv2 = gtk.TextView()
		
      vbox.add(self.tv2)
      hbox = gtk.HBox(True, 3)
		
      Set = gtk.Button("set")
      Set.set_size_request(70, 30)
		
      retrieve = gtk.Button("retrieve")
      hbox.add(Set)
      hbox.add(retrieve)
      halign = gtk.Alignment(1, 0, 0, 0)
      halign.add(hbox)
		
      vbox.pack_start(halign, False, False, 3)
      self.add(vbox)
      Set.connect("clicked", self.on_set)
      retrieve.connect("clicked", self.on_retrieve)
      self.connect("destroy", gtk.main_quit)
      self.show_all()
		
   def on_set(self, widget):
      buf = self.tv1.get_buffer()
      text = buf.get_text(buf.get_start_iter(), buf.get_end_iter())
      self.clipboard = gtk.clipboard_get()
      self.clipboard.set_text(text)
      self.clipboard.store()
		
   def on_retrieve(self, widget):
      self.clipboard.request_text(self.readclipboard, user_data=None)
		
   def readclipboard(self, clipboard, text, data):
      buffer = gtk.TextBuffer()
      buffer.set_text(text)
      self.tv2.set_buffer(buffer)

PyApp()
gtk.main()

The above code will generate the following output −

Clipboard Demo
Advertisements