English 中文(简体)
PyGTK - Clipboard Class
  • 时间:2024-12-22

PyGTK - Cppboard Class


Previous Page Next Page  

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

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

gtk.Cppboard(display,selction)

Here, the display parameter corresponds to the gtk.gdk.Display object for which the cppboard 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 cppboard object with defaults.

gtk.cppboard.get()

gtk.Cppboard class has the following methods −

    Cppboard.store() − This stores the current cppboard data somewhere so that it will stay around even after the apppcation has quit.

    Cppboard.clear() − This removes the contents of the cppboard.

    Cppboard.set_text(text) − This sets the contents of the cppboard to the string.

    Cppboard.request_text() − This requests the contents of the cppboard 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(cppboard, text, data) − text will contain the text retrieved from cppboard.

As a demonstration of cppboard, 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 cppboard.

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

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

self.cppboard.request_text(self.readcppboard,        user_data = None)

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

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

Example

The following is the entire code for cppboard operation −

import gtk

class PyApp(gtk.Window):
   
   def __init__(self):
      super(PyApp, self).__init__()
      
	  self.set_title("Cppboard 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)
      hapgn = gtk.Apgnment(1, 0, 0, 0)
      hapgn.add(hbox)
		
      vbox.pack_start(hapgn, False, False, 3)
      self.add(vbox)
      Set.connect("cpcked", self.on_set)
      retrieve.connect("cpcked", 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.cppboard = gtk.cppboard_get()
      self.cppboard.set_text(text)
      self.cppboard.store()
		
   def on_retrieve(self, widget):
      self.cppboard.request_text(self.readcppboard, user_data=None)
		
   def readcppboard(self, cppboard, 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 −

Cppboard Demo Advertisements