PyGObject GTK+ 3 - 文档?

26

PyGObject似乎没有真正的文档。 这份教程 是最接近实际情况的。我整个上午都在尝试找到Gtk.Window构造函数接受哪些参数的说明,但一直没有找到。似乎在Python中我无法进行太多的反射操作,因为PyGObject中的所有内容都是动态生成的。

我只是想知道可以传递哪些参数给这个构造函数!在GTK+ 3文档中似乎没有这个对象的等效项,而阅读源代码以了解绑定关系被证明是一个非常困难的任务。有任何建议吗?

5个回答

22

我认为这是PyGObject当前状态下的一个巨大缺陷。对于那些使用GTK+已经有一段时间的人来说没问题,但对于新用户来说可能会感到困惑。

开发人员正在致力于开发一个能够自动生成除C语言外其他语言文档的系统,该系统被称为GObject Introspection Doctools。由于该系统还没有完全准备好,因此您最好使用C API documentation并学习如何将其转换为Python。实际上并不像听起来那么难。

请记住,Python调用是动态包装到底层C库中的。你只需要学习一些东西通常如何翻译成Python并理解GTK+“属性”如何工作。这基本上是C中的一种命名约定,模式很容易学习。 PyGObject/Introspection Porting页面是一个很好的起点。

在Python中,构造函数通常被封装为C中的*_new()函数。 PyGObject还允许您在构造函数中将属于该小部件的任何GTK+属性作为关键字参数传递。因此,在Python中构造小部件时有很多选项。

您提到了GtkWindow。如果您查看GtkWindow Documentation,则可以看到gtk_window_new()函数在C中需要一个窗口类型作为参数。这将成为Python构造函数的位置参数。 PyGObject“覆盖”构造函数,以便type是可选的,并默认为顶级窗口。还有一堆GtkWindow properties可以作为关键字参数传递给构造函数。

以下是构造Python中的Gtk.Window的3个例子,它们在功能上是等效的:

# this is very close to how it's done in C using get_*/set_* accessors.
window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
window.set_title("Hello")

# setting properties as keyword arguments to the constructor
window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL, title="Hello")

# set_properties() can be used to set properties after construction
window = Gtk.Window()
window.set_properties(title="Hello")

Python交互式控制台是尝试使用小部件和属性的好方法。


谢谢,对 PyGObject 看似神秘莫测的东西提供了非常有帮助的见解。也许这种疯狂背后确实有一定的方法。哈哈 - HOLOGRAPHICpizza

16

6

稍微扩展一下接受的答案;GObject Introspection Doctools页面有一个关于如何创建自己的文档的部分。

在Ubuntu 12.04.2 LTS上,您可以执行以下命令:

$> g-ir-doc-tool --language Python -o ./output_dir /usr/share/gir-1.0/Gtk-3.0.gir
$> yelp ./output_dir/index.page

在 Linux Mint 14 上进行测试(基于 Ubuntu 12.10),您需要创建 output_dir。Yelp 观看器对我无效,但是 Yelp 构建命令有效(警告:将输出文件放置在当前目录中)。以下是构建 /usr/share/gir-1.0 中所有 gir 的 HTML 文档的快速 shell 单行程序(将 -P 9 替换为所需并行作业数量,大约等于您机器的核心数):ls /usr/share/gir-1.0 | grep '[.]gir$' | xargs -I I -n 1 -P 9 sh -c 'mkdir -p I/page && g-ir-doc-tool --language Python -o I/page /usr/share/gir-1.0/I && mkdir -p I/html && cd I/html && yelp-build html ../page' - picomancer
这是获取文档的正确方式。确保您拥有所需的所有“.gir”文件。根据fakegir,您可以使用“apt-file search ".gir" | grep -i unity”来检查“.gir”。 - Carson Ip

4
你可以使用以下方式检索对象的所有属性。
dir(YouObjectInstance.props)

YourObjectInstance是您创建的任何实例。

简单方法可能是打开终端:

you@yourcomputer ~/Desktop/python $ python
Python 2.7.2+ (default, Oct  4 2011, 20:03:08) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from gi.repository import Gtk, GtkSource, GObject
>>> window_instance = Gtk.Window()
>>> dir(window_instance.props)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__iter__', '__len__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'accept_focus', 'app_paintable', 'application', 'border_width', 'can_default', 'can_focus', 'child', 'composite_child', 'decorated', 'default_height', 'default_width', 'deletable', 'destroy_with_parent', 'double_buffered', 'events', 'expand', 'focus_on_map', 'focus_visible', 'gravity', 'halign', 'has_default', 'has_focus', 'has_resize_grip', 'has_tooltip', 'has_toplevel_focus', 'height_request', 'hexpand', 'hexpand_set', 'icon', 'icon_name', 'is_active', 'is_focus', 'margin', 'margin_bottom', 'margin_left', 'margin_right', 'margin_top', 'mnemonics_visible', 'modal', 'name', 'no_show_all', 'opacity', 'parent', 'receives_default', 'resizable', 'resize_grip_visible', 'resize_mode', 'role', 'screen', 'sensitive', 'skip_pager_hint', 'skip_taskbar_hint', 'startup_id', 'style', 'title', 'tooltip_markup', 'tooltip_text', 'transient_for', 'type', 'type_hint', 'ubuntu_no_proxy', 'urgency_hint', 'valign', 'vexpand', 'vexpand_set', 'visible', 'width_request', 'window', 'window_position']
>>> 

现在您可以立即文档化对象的属性。

如果您需要方法?

for names in dir(window_instance):
    attr = getattr(window_instance,names)
    if callable(attr):
        print names,':',attr.__doc__

如果你想要反射,可以访问这个链接:反射API。这将节省大量时间。它也可以被修改以接受任何对象或被继承。
你也可以使用: help(SomeClassModuleOrFunction) 然而,help() 输出的文本可能会受到限制,但是使用 instance.props 并循环遍历 instance 也可能存在缺陷,具体取决于代码的文档质量。
使用上述任何一种方法至少可以获得一些文档。当一个方法无法满足你的需求时,请尝试另一个方法。

尝试使用 help(SomeClassModuleOrFunction) - Gerardo Marset
Gerardo。help()已添加到答案中。它似乎是显而易见的选择,但输出质量取决于模块。虽然help()确实有其用处。 - Quentin Engles

3
使用 IPython
In [1]: from gi.repository import Gtk
In [2]: Gtk.Window()?
Type:       GObjectMeta
String Form:<class 'gi.overrides.Gtk.Window'>
File:       /usr/lib/python3/dist-packages/gi/overrides/Gtk.py
Docstring:  <no docstring>
Constructor information:
 Definition:Gtk.Window(self, type=<enum GTK_WINDOW_TOPLEVEL of type GtkWindowType>, **kwds)

了解更多细节

In [3]: help(Gtk.Window())

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接