Clipboard#

Gdk.Clipboard provides a storage area for a variety of data, including text and images. Using a clipboard allows this data to be shared between applications through actions such as copying, cutting, and pasting.

There are multiple clipboard selections for different purposes. In most circumstances, the selection named CLIPBOARD is used for everyday copying and pasting. PRIMARY is another common selection which stores text selected by the user with the cursor.

Gdk.Display will allow us to access these different clipboards. You can get the default display with Gdk.Display.get_default(), then get the clipboard that you want; Gdk.Display.get_clipboard() for CLIPBOARD and Gdk.Display.get_primary_clipboard() for PRIMARY.

You can set any simple value like text with Gdk.Clipboard.set(). For more complex data you must use the Gdk.Clipboard.set_content() method were you have to pass a Gdk.ContentProvider. A content provider is usually created using Gtk.ContentProvider.new_for_value() where you only pass the value.

To read the clipboard values Gdk.Clipboard provides three async methods. For textual or image data use Gdk.Clipboard.read_text_async() or Gdk.Clipboard.read_texture_async(). For other data use Gdk.Clipboard.read_value_async().

Example#

../_images/clipboard.png
 1import gi
 2
 3gi.require_version('Gdk', '4.0')
 4gi.require_version('Gtk', '4.0')
 5from gi.repository import Gdk, Gtk
 6
 7
 8class ClipboardWindow(Gtk.ApplicationWindow):
 9    def __init__(self, *args, **kargs):
10        super().__init__(*args, **kargs, title='Clipboard Example')
11
12        box = Gtk.Box(spacing=12, orientation=Gtk.Orientation.VERTICAL)
13        self.set_child(box)
14
15        self.clipboard = Gdk.Display.get_default().get_clipboard()
16
17        text_box = Gtk.Box(spacing=6, homogeneous=True)
18        box.append(text_box)
19
20        self.entry = Gtk.Entry(text='Some text you can copy')
21        button_copy_text = Gtk.Button(label='Copy Text')
22        button_copy_text.connect('clicked', self.copy_text)
23        button_paste_text = Gtk.Button(label='Paste Text')
24        button_paste_text.connect('clicked', self.paste_text)
25
26        text_box.append(self.entry)
27        text_box.append(button_copy_text)
28        text_box.append(button_paste_text)
29
30        image_box = Gtk.Box(spacing=6)
31        box.append(image_box)
32
33        self.picture = Gtk.Picture.new_for_filename(
34            '../images/application.png'
35        )
36        self.picture.props.hexpand = True
37        button_copy_image = Gtk.Button(
38            label='Copy Image', valign=Gtk.Align.CENTER
39        )
40        button_copy_image.connect('clicked', self.copy_image)
41        button_paste_image = Gtk.Button(
42            label='Paste Image', valign=Gtk.Align.CENTER
43        )
44        button_paste_image.connect('clicked', self.paste_image)
45
46        image_box.append(self.picture)
47        image_box.append(button_copy_image)
48        image_box.append(button_paste_image)
49
50    def copy_text(self, _button):
51        self.clipboard.set(self.entry.get_text())
52
53    def paste_text(self, _button):
54        self.clipboard.read_text_async(None, self.on_paste_text)
55
56    def on_paste_text(self, _clipboard, result):
57        text = self.clipboard.read_text_finish(result)
58        if text is not None:
59            self.entry.set_text(text)
60
61    def copy_image(self, _button):
62        texture = self.picture.get_paintable()
63        gbytes = texture.save_to_png_bytes()
64        content = Gdk.ContentProvider.new_for_bytes('image/png', gbytes)
65        self.clipboard.set_content(content)
66
67    def paste_image(self, _button):
68        self.clipboard.read_texture_async(None, self.on_paste_image)
69
70    def on_paste_image(self, _clipboard, result):
71        texture = self.clipboard.read_texture_finish(result)
72        if texture is not None:
73            self.picture.set_paintable(texture)
74
75
76def on_activate(app):
77    win = ClipboardWindow(application=app)
78    win.present()
79
80
81app = Gtk.Application(application_id='com.example.App')
82app.connect('activate', on_activate)
83
84app.run(None)