2012-03-15 25 views
0

私はgtkウィジェットを持っています。その子孫の中に別のウィジェットがあるかどうかを調べたいと思います。 1つがある場合はそれを返したい。これは単純な再帰的な問題ですが、私はそれを行う正しい方法を得ることができません。空き地のXMLファイルにpygtk親ノードから名前でウィジェットを取得する

、私が持っている:

<object class="GtkDialog" id="monkey"> 
    [...] 
     <object class="GtkTreeView" id="ook"> 

find(my_monkey_object, 'ook')への呼び出しがGtkTreeViewオブジェクトを返す必要があります。 find()は、私が使用する必要がXXX()メソッドを確認していない

def find (node, id): 
    if node.XXX() == id: return node 
    for child in node.get_children(): 
     ret = find(child, id) 
     if ret: return ret 
    return None 

に似て何かをする必要があります。 get_name()は期待通りに見えましたが、 "id"ではなくオブジェクトのクラス名を返します。私が使用しているバージョンはpygtk-2.24です。

同じ問題がこのPython GTK+ widget nameの質問を参照してください。

このbugの種類で問題が説明されています。ビルダーIDはGTKウィジェットツリーのようにしたいと思います。残念ながら、これは得ることが不可能だ...

+1

:ようpygiを使用するようになって

、それが見えます。 – ergosys

+1

'gtk.Buildable(widget).get_name()'はコメントごとに動作しません19そのバグは? – ergosys

答えて

5

gtkのC-APIドキュメントによると、あなたはこのような空き地 "ID" の名前を取得することができます:PyGTKのために

name = gtk_buildable_get_name (GTK_BUILDABLE (widget)) 

が、これはそれを持っている

name = gtk.Buildable.get_name(widget) 
+0

本当にありがとうございます。 – Sardathrion

+0

@ergosys @Sardathrion 'name'はポインタですか?' widget'はウィジェット名ですか? – inckka

1

あなたのノードオブジェクトはgtk.Container派生クラスですね。多分isinstance(node, gtk.TreeView)があなたが探しているものです。 gtk.Widgetサブクラスには「id」自体はありません。 idフィールドはglade-xmlパーサーに属します。

def find_child_classes(container, cls): 
    return [widget for widget in container.get_children() if isinstance(widget, cls)] 

それともビルダーオブジェクトを維持して、インスタンスにアクセスします:builder.get_object('your-object-id')

は、私のような何かを提案することができます。

+0

あなたの仮定は本当に正しいです:ノードはgtk.Container派生クラスです。 'isinstance()'メソッドは、 "ook"という名前だけでなく、gtl.TreeViewと一致するので理想的ではありません。 – Sardathrion

0

This answerと同じです。私はそれがPythonで動作しますが、GTKのドキュメントによると、あなたはgtk_buildable_get_nameから「ID」の名前を得ることができるかどうかはわかりません

# Copypasta from https://stackoverflow.com/a/20461465/2015768 
# http://cdn.php-gtk.eu/cdn/farfuture/riUt0TzlozMVQuwGBNNJsaPujRQ4uIYXc8SWdgbgiYY/mtime:1368022411/sites/php-gtk.eu/files/gtk-php-get-child-widget-by-name.php__0.txt 
# note get_name() vs gtk.Buildable.get_name(): https://stackoverflow.com/questions/3489520/python-gtk-widget-name 
def get_descendant(widget, child_name, level, doPrint=False): 
    if widget is not None: 
    if doPrint: print("-"*level + ": " + (Gtk.Buildable.get_name(widget) or "(None)") + " :: " + (widget.get_name() or "(None)")) 
    else: 
    if doPrint: print("-"*level + ": " + "None") 
    return None 
    #/*** If it is what we are looking for ***/ 
    if(Gtk.Buildable.get_name(widget) == child_name): # not widget.get_name() ! 
    return widget; 
    #/*** If this widget has one child only search its child ***/ 
    if (hasattr(widget, 'get_child') and callable(getattr(widget, 'get_child')) and child_name != ""): 
    child = widget.get_child() 
    if child is not None: 
     return get_descendant(child, child_name,level+1,doPrint) 
    # /*** Ity might have many children, so search them ***/ 
    elif (hasattr(widget, 'get_children') and callable(getattr(widget, 'get_children')) and child_name !=""): 
    children = widget.get_children() 
    # /*** For each child ***/ 
    found = None 
    for child in children: 
     if child is not None: 
     found = get_descendant(child, child_name,level+1,doPrint) # //search the child 
     if found: return found 
関連する問題