Wednesday, 21 August 2013

Unable to get text from the X11 clipboard

Unable to get text from the X11 clipboard

I'm essentially trying to run the accepted answer in the question "getting
HTML source or rich text from the X clipboard", however, I get a slew of
errors. The exact code I'm using has been slightly updated for Python 3
and GTK+-3, so here is what I've got:
#!/usr/bin/python
from gi.repository import Gtk as gtk
from gi.repository import GLib as glib
def test_clipboard():
clipboard = gtk.Clipboard()
targets = clipboard.wait_for_targets()
print("Targets available:", ", ".join(map(str, targets)))
for target in targets:
print("Trying '%s'..." % str(target))
contents = clipboard.wait_for_contents(target)
if contents:
print(contents.data)
def main():
mainloop = glib.MainLoop()
def cb():
test_clipboard()
mainloop.quit()
glib.idle_add(cb)
mainloop.run()
if __name__ == "__main__":
main()
And when I run it:
$ python clip_test.py
Gtk-Message: Failed to load module "gnomesegvhandler"
(clip_test.py:17736): Gdk-CRITICAL **:
gdk_display_supports_selection_notification: assertion `GDK_IS_DISPLAY
(display)' failed
/usr/lib64/python3.2/site-packages/gi/types.py:43: Warning:
g_object_get_data: assertion `G_IS_OBJECT (object)' failed
return info.invoke(*args, **kwargs)
(clip_test.py:17736): Gdk-CRITICAL **: gdk_display_get_default_screen:
assertion `GDK_IS_DISPLAY (display)' failed
(clip_test.py:17736): Gtk-CRITICAL **: gtk_invisible_new_for_screen:
assertion `GDK_IS_SCREEN (screen)' failed
/usr/lib64/python3.2/site-packages/gi/types.py:43: Warning: invalid (NULL)
pointer instance
return info.invoke(*args, **kwargs)
/usr/lib64/python3.2/site-packages/gi/types.py:43: Warning:
g_signal_connect_data: assertion `G_TYPE_CHECK_INSTANCE (instance)' failed
return info.invoke(*args, **kwargs)
(clip_test.py:17736): Gtk-CRITICAL **: gtk_widget_add_events: assertion
`GTK_IS_WIDGET (widget)' failed
/usr/lib64/python3.2/site-packages/gi/types.py:43: Warning:
g_object_set_data: assertion `G_IS_OBJECT (object)' failed
return info.invoke(*args, **kwargs)
/usr/lib64/python3.2/site-packages/gi/types.py:43: Warning:
g_object_set_qdata: assertion `G_IS_OBJECT (object)' failed
return info.invoke(*args, **kwargs)
(clip_test.py:17736): Gdk-CRITICAL **: gdk_display_get_default_screen:
assertion `GDK_IS_DISPLAY (display)' failed
(clip_test.py:17736): Gtk-CRITICAL **: gtk_invisible_new_for_screen:
assertion `GDK_IS_SCREEN (screen)' failed
(clip_test.py:17736): Gtk-CRITICAL **: gtk_widget_add_events: assertion
`GTK_IS_WIDGET (widget)' failed
(clip_test.py:17736): Gtk-CRITICAL **: gtk_widget_get_window: assertion
`GTK_IS_WIDGET (widget)' failed
(clip_test.py:17736): Gtk-CRITICAL **: gtk_selection_convert: assertion
`GTK_IS_WIDGET (widget)' failed
…and it never returns. (Even if I highlight and copy something to the
clipboard.) The error:
assertion `GDK_IS_DISPLAY (display)' failed
Seems to be that it can't find my display, but if I do:
import os
print('DISPLAY = ' + repr(os.environ['DISPLAY']))
…then I get: DISPLAY = ':0.0'

No comments:

Post a Comment