簡體   English   中英

PyGObject GTK + 3-文檔?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM