python and gtk3 clipboard onChange

2019-06-18 15:52发布

问题:

With PyGTK 2 I could attach a function to be executed when the contents of the clipboard was changed. Browsing through the documentation of GTK3's python bindings I can not find any description of such an functionality.

Can anyone tell me the 'best practice' for this?

EDIT

With gtk2 the following works:

import gtk

def test(*args):
  print "Clipboard changed"

clip = gtk.Clipboard()
clip.connect('owner-change',test)

When adopting to Gtk3

from gi.repository import Gtk, Gdk

def test(*args):
  print "Clipboard changed"

clip = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clip.Connect('owner-change',test)

Python accepts the connection to the signal, but my function is never executed.

回答1:

from gi.repository import Gtk, Gdk

def test(*args):
    print "Clipboard changed"

clip = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
clip.connect('owner-change',test)
Gtk.main()

works for me.