Kivy - Clipboard



The Clipboard object in Kivy framework gives access to the clipboard of the operating system being used. With the help of Kivy's Clipboard object, one can perform programmatically the cut, copy and paste operations.

The clipboard is a temporary buffer in computer's RAM that most operating systems provide for short-term storage and transfer within and between application programs. In the operating system, this clipboard is a global object. Most operating systems use conventional keyboard shortcuts for carrying out cut, copy and paste data between applications.

Normally, the explicit use of the cut-copy-paste operations through the Clipboard is not necessary. However, in some cases it may be useful.

The Clipboard object is defined in the "kivy.core.clipboard" module. The following methods are available for Clipboard object −

  • copy() − Copy the value provided in argument data into current clipboard. If data is not of type string it will be converted to string.

  • get() − Get the current data in clipboard, using the mimetype if possible. You not use this method directly. Use paste method instead.

  • get_types() − Return a list of supported mimetypes.

  • paste() − Get text from the system clipboard and return it a usable string.

  • put() − Put data on the clipboard, and attach a mimetype. You should not use this method directly. Use copy method instead.

Example

In the following example, we have two multiline textboxes and two buttons arranged in a BoxLayout. The COPY button calls gettext() method which copies the selected text from the upper text box to the clipboard.

def gettext(self, instance):
   Clipboard.copy(data=self.text1.selection_text)

The PASTE button invokes a callback insert() which pastes the selected text at the cursor position.

def insert(self, instance):
   txt = Clipboard.paste()
   print (txt)
   self.text2.insert_text(txt)

These two functions are bound to two buttons −

self.b1=Button(text='COPY')
self.b1.bind(on_press=self.gettext)
self.b2=Button(text='PASTE')
self.b2.bind(on_press=self.insert)

The build() method assembles the text boxes and buttons.

Here is the complete code for this exercise −

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.core.clipboard import Clipboard
from kivy.core.window import Window

Window.size = (720, 400)

class mydemoapp(App):
   def gettext(self, instance):
      Clipboard.copy(data=self.text1.selection_text)
      
   def insert(self, instance):
      txt = Clipboard.paste()
      print(txt)
      self.text2.insert_text(txt)
      
   def build(self):
      main = BoxLayout(orientation='vertical')
      self.text1 = TextInput(multiline=True, font_size=32)
      btns = BoxLayout(orientation='horizontal')
      self.b1 = Button(text='COPY')
      self.b1.bind(on_press=self.gettext)
      self.b2 = Button(text='PASTE')
      self.b2.bind(on_press=self.insert)
      self.text2 = TextInput(
         multiline=True, font_size=32,
         foreground_color=[0, 0, 1, 1]
      )
      btns.add_widget(self.b1)
      btns.add_widget(self.b2)
      main.add_widget(self.text1)
      main.add_widget(btns)
      main.add_widget(self.text2)
      return main
      
mydemoapp().run()

Output

When the program is run, you will observe two textboxes. Enter Simple is better than Complex in the upper box, and Complex is Complicated in the lower box.

Then, select the substring better than, and click the COPY button to store it on the clipboard. Click besides the word Complicated in the lower box, and click the PASTE button. The text on the clipboard will be inserted.

Kivy Clipboard
Advertisements