[英]PyGObject GTK+ 3 - Documentation?
PyGObject似乎没有真实的文档。 本教程尽可能接近。 我Gtk.Window
都在努力地寻找Gtk.Window
构造函数接受的参数的描述。 似乎我无法在Python中做很多反思,因为PyGObject中的所有内容都是动态生成的。
我要知道的是可以传递给此构造函数的参数! 在GTK + 3文档中似乎没有与之等效的对象,并且阅读源代码以找出绑定被证明是一项极为艰巨的任务。 有任何想法吗??
我同意这是当前状态下PyGObject的巨大缺陷。 对于已经使用GTK +一段时间的我们来说,这没问题,但是对于新用户而言,这可能会令人困惑。
人们正在一个系统上自动为非 C语言(称为GObject Introspection Doctools)生成文档。 既然还没有准备好,那么最好使用C API文档并学习如何将其转换为Python。 它并不像听起来那样难。
请记住,Python调用是动态包装到基础C库的。 您需要做的就是学习通常如何将一些内容转换为Python,并了解GTK +“属性”的工作方式。 它基本上是C的命名约定,而且模式很容易学习。 PyGObject / Introspection移植页面是一个好的开始。
Python中的构造函数通常包装到C中的*_new()
函数。PyGObject还允许您传入属于该小部件的任何 GTK +属性,作为构造函数中的关键字参数。 因此,在Python中构造小部件时,您有很多选择。
您已经提到了GtkWindow
。 如果查看GtkWindow文档 ,则gtk_window_new()
函数将窗口类型作为C中的参数。这将是Python中构造函数的位置参数。 PyGObject“覆盖”构造函数,以便该type
是可选的,并且默认为顶级窗口。 有许多GtkWindow属性也可以作为关键字参数传递给构造函数。
这是3个在Python中构造Gtk.Window
示例,它们在功能上是Gtk.Window
:
# 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
您可以使用此方法检索对象的所有属性
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__
如果您需要反射,则可以转到以下链接: Reflection api ,它将节省大量时间。 也可以将其修改为接受任何对象或被继承。
您还可以使用: help(SomeClassModuleOrFunction)
虽然可以限制来自help()的打印文本,但是使用instance.props并在实例上循环也可能会有短时间,具体取决于代码的文档编制情况。
使用以上任何一种方法至少可以获得一些文档。 当一个不合适时,请尝试另一个。
使用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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.