简体   繁体   English

从gi.repository导入Gtk对象

[英]Importing Gtk object from gi.repository

Back in my GTK2 days, I could do a 回到我的GTK2天,我可以做一个

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: 我一直在挠头,看看是否有办法像这样从Gtk导入对象:

from gi.repository import Gtk.Clipboard

of course this results in a SyntaxError exception. 当然,这会导致SyntaxError异常。

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. 我不想导入所有的Gtk,因为它过高/过大。 I just want the Clipboard class, but I'm curious for other items in the gi.repository (like Notify). 我只想要Clipboard类,但对gi.repository中的其他项(例如Notify)感到好奇。

Thanks, 谢谢,

Narnie 纳妮

非常简单地。

from gi.repository.Gtk import Clipboard

you can't import just an object: you can import a module (Gtk) and add a name (Clipboard) into current namespace ie, these should be equivalent: 您不能只导入一个对象:您可以导入模块(Gtk)并在当前名称空间中添加一个名称(Clipboard),即,这些名称应等效:

from gi.repository.Gtk import Clipboard

and

from gi.repository import Gtk
Clipboard = Gtk.Clipboard

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM