PyGObject似乎没有真正的文档。 这份教程 是最接近实际情况的。我整个上午都在尝试找到Gtk.Window
构造函数接受哪些参数的说明,但一直没有找到。似乎在Python中我无法进行太多的反射操作,因为PyGObject中的所有内容都是动态生成的。
我只是想知道可以传递哪些参数给这个构造函数!在GTK+ 3文档中似乎没有这个对象的等效项,而阅读源代码以了解绑定关系被证明是一个非常困难的任务。有任何建议吗?
我认为这是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可以作为关键字参数传递给构造函数。
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交互式控制台是尝试使用小部件和属性的好方法。
这里是文档位置: https://lazka.github.io/pgi-docs/Gtk-3.0/index.html
Gtk.Window 参数(确切地说,这就是您所要求的)在此处: https://lazka.github.io/pgi-docs/Gtk-3.0/classes/Window.html
有一些交互式控制台解决方案可以上面找到,但我更喜欢自动补全的那个: 如何将选项卡补全添加到Python shell中?
稍微扩展一下接受的答案;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
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 Ipdir(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__
help(SomeClassModuleOrFunction)
。 - Gerardo MarsetIn [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())