Question

Is there a way to get notified of clipboard change in KDE/GNOME via D-Bus? How can I capture/eavesdrop/subscribe to clipboard selection (esp. in PyGTK)? I have gtk.Clipboard.wait_for_text() method available but it returns clipboard contents immediately while I need to invoke it only when clipboard changes.

Was it helpful?

Solution

Got that pretty much here: python and gtk3 clipboard onChange

The core of the problem is:

self.clipboard = gtk.Clipboard()
self.clipboard.connect("owner-change", self.renderText)

It's black magic to me where this owner-change signal came from or how to find what signal is responsible for particular event (understood as gui phenomena) but it works:

#!/usr/bin/env python

import gtk, glib

class ClipboardParse:
    def __init__(self):
        window = gtk.Window()
        window.set_title("example")
        window.resize(600,400)
        box = gtk.HBox(homogeneous = True, spacing = 2)
        self.buf = gtk.TextBuffer()
        textInput = gtk.TextView(self.buf)
        self.lbl = gtk.Label()
        box.add(self.lbl)
        window.add(box)
        window.connect("destroy", gtk.main_quit)
        window.show_all()
        self.clipboard = gtk.Clipboard()
        self.clipboard.connect("owner-change", self.renderText)

    def renderText(self, clipboard, event):
        print 'C {0} | E {1}'.format(clipboard, event)
        txt = self.clipboard.wait_for_text()
        self.lbl.set_text(txt)
        print txt
        return False

if __name__ == '__main__':
    ClipboardParse()
    gtk.main()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top