0

Back in my GTK2 days, I could do a

from gtk import Clipboard

To get access to the clipboard from my program.

Now days, we must import objects introspectively as in:

from gi.repository import Gtk

I have been scratching my head to see if there is a way to import just an object from Gtk like:

from gi.repository import Gtk.Clipboard

of course this results in a SyntaxError exception.

Is there a way to do what I'm trying to do? One reason I would like this right now, is that I need a very light way to access the clipboard for a script. I don't want to import all of Gtk, as it is overboard/overkill. I just want the Clipboard class, but I'm curious for other items in the gi.repository (like Notify).

Thanks,

Narnie

narnie
  • 1,742
  • 1
  • 18
  • 34

2 Answers2

2

Very easily.

from gi.repository.Gtk import Clipboard
Fredrick Brennan
  • 7,079
  • 2
  • 30
  • 61
  • Wonderful! I had searched for syntax for from..import, but never found that you could do it that way (I still don't understand fully how gi.repo works, but I see it must be like other modules in subfolders). I should have thought of trying this!!! Much appreciated. – narnie Jan 19 '13 at 22:26
  • 1
    @narnie No problem :) Sometimes it's the obvious things that slow us down – Fredrick Brennan Jan 19 '13 at 22:28
1

you can't import just an object: you can import a module (Gtk) and add a name (Clipboard) into current namespace i.e., these should be equivalent:

from gi.repository.Gtk import Clipboard

and

from gi.repository import Gtk
Clipboard = Gtk.Clipboard
jfs
  • 399,953
  • 195
  • 994
  • 1,670