6

In Python 2, with what's installed in my machine (Ubuntu 12.10), I can do

import gtk
cb = gtk.clipboard_get()
content = cb.wait_for_text()

to get the text from the clipboard.

In Python3, however, I was supposed to do the same with the Gtk.Clipboard() object, but I get thrown some errors that seem to be related to widgets and things like that, but my application has no GUI and is not supposed to. Am I on the right track?

>>> from gi.repository import Gtk
>>> cb = Gtk.Clipboard()
>>> content = cb.wait_for_text()
/usr/lib/python3/dist-packages/gi/types.py:47: Warning: g_object_get_data: assertion `G_IS_OBJECT (object)' failed
  return info.invoke(*args, **kwargs)

(.:10935): Gdk-CRITICAL **: gdk_display_get_default_screen: assertion `GDK_IS_DISPLAY (display)' failed

(.:10935): Gtk-CRITICAL **: gtk_invisible_new_for_screen: assertion `GDK_IS_SCREEN (screen)' failed
/usr/lib/python3/dist-packages/gi/types.py:47: Warning: invalid (NULL) pointer instance
  return info.invoke(*args, **kwargs)
/usr/lib/python3/dist-packages/gi/types.py:47: Warning: g_signal_connect_data: assertion `G_TYPE_CHECK_INSTANCE (instance)' failed
  return info.invoke(*args, **kwargs)

(.:10935): Gtk-CRITICAL **: gtk_widget_add_events: assertion `GTK_IS_WIDGET (widget)' failed
/usr/lib/python3/dist-packages/gi/types.py:47: Warning: g_object_set_data: assertion `G_IS_OBJECT (object)' failed
  return info.invoke(*args, **kwargs)
/usr/lib/python3/dist-packages/gi/types.py:47: Warning: g_object_set_qdata: assertion `G_IS_OBJECT (object)' failed
  return info.invoke(*args, **kwargs)

(.:10935): Gdk-CRITICAL **: gdk_display_get_default_screen: assertion `GDK_IS_DISPLAY (display)' failed

(.:10935): Gtk-CRITICAL **: gtk_invisible_new_for_screen: assertion `GDK_IS_SCREEN (screen)' failed

(.:10935): Gtk-CRITICAL **: gtk_widget_add_events: assertion `GTK_IS_WIDGET (widget)' failed

(.:10935): Gtk-CRITICAL **: gtk_widget_get_window: assertion `GTK_IS_WIDGET (widget)' failed

(.:10935): Gtk-CRITICAL **: gtk_selection_convert: assertion `GTK_IS_WIDGET (widget)' failed
Pablo
  • 652
  • 6
  • 21

1 Answers1

5

You aren't just dealing with Python 2 and Python 3 -- in fact that is irrelevant in this case. What does matter is that the first example is using PyGTK which is GTK2 and the second is using PyGObject which is GTK3.

Since GTK3 is using gobject introspection with Python, some of the magic that was manually maintained in PyGTK is not there. If you look at the C API for the Clipboard you will see that Gtk.Clipboard.get() has a required argument to identify the clipboard. This is explained in the reference for gtk_clipboard_get_for_display().

To make a long story short, the correct code in GTK3 is more like this:

from gi.repository import Gtk, Gdk
cb = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
content = cb.wait_for_text()
Micah Carrick
  • 9,967
  • 3
  • 31
  • 43
  • Hello, Micah. I'm aware of the parameter to the function, but I'm basing myself on some examples I saw on the internet (which are supposed to work). Not only examples in Python, but in Vala. Anyway, Gtk.Clipboard(Gdk.SELECTION_CLIPBOARD) throws a type error: – Pablo Nov 17 '12 at 00:54
  • >>> cb = Gtk.Clipboard(Gdk.SELECTION_CLIPBOARD) Traceback (most recent call last): File "", line 1, in TypeError: GObject.__init__() takes exactly 0 arguments (1 given) – Pablo Nov 17 '12 at 00:54
  • I'm sorry, I meant `cb = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)`. There is no constructor as there is no corresponding `gtk_clipboard_new`. – Micah Carrick Nov 17 '12 at 05:08