2012-03-15 1 views
0

저는 gtk 위젯을 가지고 있는데, 그 하위 노드 내에 다른 위젯이 있는지 알아보고 싶습니다. 있을 경우 반환하고 그렇지 않으면 반환하지 않습니다. 이것은 단순한 재귀적인 문제이지만 올바른 방법을 사용하지 못하는 것 같습니다. 숲 사이의 빈터 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과 동일 . 이 파이썬에서 작동하지만 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 
관련 문제