File: clipboard.py

package info (click to toggle)
pygobject 3.54.5-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,864 kB
  • sloc: ansic: 40,281; python: 26,363; sh: 477; makefile: 81; xml: 35; cpp: 1
file content (78 lines) | stat: -rw-r--r-- 2,661 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import gi

gi.require_version("Gdk", "4.0")
gi.require_version("Gtk", "4.0")
from gi.repository import Gdk, Gtk


class ClipboardWindow(Gtk.ApplicationWindow):
    def __init__(self, *args, **kargs):
        super().__init__(*args, **kargs, title="Clipboard Example")

        box = Gtk.Box(spacing=12, orientation=Gtk.Orientation.VERTICAL)
        self.set_child(box)

        self.clipboard = Gdk.Display.get_default().get_clipboard()

        text_box = Gtk.Box(spacing=6, homogeneous=True)
        box.append(text_box)

        self.entry = Gtk.Entry(text="Some text you can copy")
        button_copy_text = Gtk.Button(label="Copy Text")
        button_copy_text.connect("clicked", self.copy_text)
        button_paste_text = Gtk.Button(label="Paste Text")
        button_paste_text.connect("clicked", self.paste_text)

        text_box.append(self.entry)
        text_box.append(button_copy_text)
        text_box.append(button_paste_text)

        image_box = Gtk.Box(spacing=6)
        box.append(image_box)

        self.picture = Gtk.Picture.new_for_filename("../images/application.png")
        self.picture.props.hexpand = True
        button_copy_image = Gtk.Button(label="Copy Image", valign=Gtk.Align.CENTER)
        button_copy_image.connect("clicked", self.copy_image)
        button_paste_image = Gtk.Button(label="Paste Image", valign=Gtk.Align.CENTER)
        button_paste_image.connect("clicked", self.paste_image)

        image_box.append(self.picture)
        image_box.append(button_copy_image)
        image_box.append(button_paste_image)

    def copy_text(self, _button):
        self.clipboard.set(self.entry.get_text())

    def paste_text(self, _button):
        self.clipboard.read_text_async(None, self.on_paste_text)

    def on_paste_text(self, _clipboard, result):
        text = self.clipboard.read_text_finish(result)
        if text is not None:
            self.entry.set_text(text)

    def copy_image(self, _button):
        texture = self.picture.get_paintable()
        gbytes = texture.save_to_png_bytes()
        content = Gdk.ContentProvider.new_for_bytes("image/png", gbytes)
        self.clipboard.set_content(content)

    def paste_image(self, _button):
        self.clipboard.read_texture_async(None, self.on_paste_image)

    def on_paste_image(self, _clipboard, result):
        texture = self.clipboard.read_texture_finish(result)
        if texture is not None:
            self.picture.set_paintable(texture)


def on_activate(app):
    win = ClipboardWindow(application=app)
    win.present()


app = Gtk.Application(application_id="com.example.App")
app.connect("activate", on_activate)

app.run(None)