python - 对 pygtk3 的自省(introspection)可能吗?

标签 python pygtk pygobject gtk3

python 的一大优点是能够对方法和函数进行自省(introspection)。例如,要获取 math.log 的函数签名,您可以(在 ipython 中)运行此命令:

In [1]: math.log?
Type:       builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form:    <built-in function log>
Namespace:  Interactive
Docstring:
    log(x[, base])

    Return the logarithm of x to the given base.
    If the base not specified, returns the natural logarithm (base e) of x.

并且看到 x 和可选的 base 是这个函数的参数。

有了新的 gtk3 和 automaticall generated pygobject bindings ,在我尝试过的所有示例中,我只能将 (*args, **kwargs) 作为每个 gtk 方法的参数。

示例:Label.set_text which requires a string :

In [1]: from gi.repository import Gtk
In [2]: mylabel = Gtk.Label("hello")
In [3]: mylabel.set_text?
Type:       instancemethod
Base Class: <type 'instancemethod'>
String Form:    <bound method Label.set_text of <Label object at 0x275b230 (GtkLabel at 0x28cd040)>>
Namespace:  Interactive
File:       /usr/lib/python2.7/dist-packages/gi/types.py
Definition: L.set_text(*args, **kwargs)
Docstring:
    <no docstring>

现在的问题是:这个(python 绑定(bind)的方法自省(introspection)的丢失)是否会再次改变(文档)工作已经进入 pygobjects,或者这是由于 pygobjects 的工作方式而保留下来的东西?

最佳答案

现在,我认为这还没有准备好。但是,您仍然可以通过查看 Gtk-3.0.gir 进行手动内省(introspection)。文件(在我的系统中位于 /usr/share/gir-1.0/Gtk-3.0.gir )。

gir 文件只是一个 xml 文件,无论您使用何种编程语言,它都应该完全用于探索公开的界面。例如,Label类可以找到寻找 <class name="Label" . class里面标记有一个 doc带有有关此小部件应该做什么的大量信息的标记。还有很多method标签,其中之一是您对您感兴趣的示例:<method name="set_text" .这里面method标签不仅有 doc描述方法的标签,还有一个 parameters依次包含一些 parameter 的标签用于根据名称、描述和类型来描述每个参数的标签:

<parameters>
  <parameter name="str" transfer-ownership="none">
    <doc xml:whitespace="preserve">The text you want to set</doc>
    <type name="utf8" c:type="gchar*"/>
  </parameter>
</parameters>

所以所有的信息都已经存在了。

关于python - 对 pygtk3 的自省(introspection)可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7859490/

相关文章:

python - Windows python 上的 Utf-8

python - 使用 PyGTK 进行线程化

python - 禁用 gtkbutton 的发光效果

python - PyGTK:访问 gtk.MessageDialog 中的按钮?

glib - GLib 和 GObject 到底是什么?

python - Snakefile 的各种迭代都会给出相同的错误

python - 向 txt 文件添加一行

python - 对 datetime.datetime 二维数组进行排序

python - 编辑 GtkWidget 属性/特性

python - gtk+2、gtk+3 和 pygobject3 全部安装到 mac 后为 "No module named pygtk found"