Python pygtk从父节点按名称获取小部件

Python pygtk从父节点按名称获取小部件,python,gtk,pygtk,Python,Gtk,Pygtk,我有一个gtk小部件,我想知道在它的子代中是否还有另一个小部件。如果有,我想退货,否则不退货。这是一个简单的递归问题,但我似乎找不到正确的方法来解决它 在glade xml文件中,我有: <object class="GtkDialog" id="monkey"> [...] <object class="GtkTreeView" id="ook"> 我不确定我需要使用哪个XXX()方法get_name()返回对象的类名,而不是其“id”。我使用的

我有一个gtk小部件,我想知道在它的子代中是否还有另一个小部件。如果有,我想退货,否则不退货。这是一个简单的递归问题,但我似乎找不到正确的方法来解决它

在glade xml文件中,我有:

  <object class="GtkDialog" id="monkey">
  [...]
       <object class="GtkTreeView" id="ook">
我不确定我需要使用哪个XXX()方法
get_name()
返回对象的类名,而不是其“id”。我使用的版本是pygtk-2.24

同样的问题请看这个问题


注意,这类代码解释了这个问题:我希望生成器ID来自GTK小部件树。遗憾的是,这似乎不可能得到…

我猜您的节点对象是一个gtk.Container派生的类。也许
isinstance(node,gtk.TreeView)
就是您要寻找的。在
gtk.Widget
-子类中没有“id”本身。id字段属于gladexml解析器

我可否提出以下建议:

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

或者保留builder对象并通过以下方式访问实例:
builder.get_object('your-object-id')

我猜您的节点对象是gtk.Container派生的类。也许
isinstance(node,gtk.TreeView)
就是您要寻找的。在
gtk.Widget
-子类中没有“id”本身。id字段属于gladexml解析器

我可否提出以下建议:

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

或者通过以下方式保留生成器对象并访问实例:
builder.get_object('your-object-id')

根据gtk C-api文档,您可以获得glade“id”名称,如下所示:

name = gtk_buildable_get_name (GTK_BUILDABLE (widget))
对于pygtk,这与

name = gtk.Buildable.get_name(widget)

根据gtk C-api文档,您可以获得如下glade“id”名称:

name = gtk_buildable_get_name (GTK_BUILDABLE (widget))
对于pygtk,这与

name = gtk.Buildable.get_name(widget)

适应于使用pygi,它看起来像:

# 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

适应于使用pygi,它看起来像:

# 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

您的假设确实正确:node是一个gtk.Container派生的类。
isinstance()
方法并不理想,因为它将匹配所有gtl.TreeView,而不仅仅是名为“ook”的方法。您的假设确实正确:node是gtk.Container派生的类。
isinstance()
方法并不理想,因为它将匹配所有gtl.TreeView,而不仅仅是名为“ook”的方法。我不确定它在python中如何工作,但根据gtk文档,您可以从gtk_buildable_get_name.Does
gtk.buildable(widget).get_name()获得“id”名称
根据该bug的注释19不起作用?我不确定它在python中是如何工作的,但根据gtk文档,您可以从gtk_buildable_get_name.Does
gtk.buildable(widget.get_name()中获取“id”名称
根据该bug的注释19不起作用?@ergosys@Sardathrion所以
名称
是指针?而
小部件
是小部件名称?@ergosys@Sardathrion所以
名称
是指针?而
小部件
是小部件名称?