如何递归遍历 PyGtk 中的所有 Gtk 子项? [英] How do I iterate through all Gtk children in PyGtk recursively?

查看:49
本文介绍了如何递归遍历 PyGtk 中的所有 Gtk 子项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 pygtk 获得主窗口的所有 Gtk 子对象的递归列表.我怎么做?

I would like to get a recursive list of all Gtk children object of the main window with pygtk. How do I do that?

推荐答案

注意这些:

... 这是一个函数,它是来自 按名称获取后代(子)小部件 |PHP-GTK 社区:

... here is a function, which is a port of a PHP one from Getting a descendant (child) widget by name | PHP-GTK Community:

# 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) + " :: " + widget.get_name())
  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

if (window):
  window.connect("destroy", gtk.main_quit)
  #pprint(inspect.getmembers(window.get_children()[0]))
  print "E: " + str( get_descendant(window, "nofind", level=0, doPrint=True) )

用法:

print "E: " + str( get_descendant(window, "nofind", level=0, doPrint=True) )
# output:

# window1 :: GtkWindow
# -scrolledwindow1 :: GtkScrolledWindow
# --viewport1 :: GtkViewport
# ---vbox1 :: GtkVBox
# ----handlebox1 :: GtkHandleBox
# -----drawingarea1 :: GtkDrawingArea
# ----handlebox2 :: GtkHandleBox
# ----handlebox3 :: GtkHandleBox
# E: None

print "E: " + str( get_descendant(window, "viewport1", level=0, doPrint=True) )
# output:

# window1 :: GtkWindow
# -scrolledwindow1 :: GtkScrolledWindow
# --viewport1 :: GtkViewport
# E: <gtk.Viewport object at 0x96cadc4 (GtkViewport at 0x95278d0)>

这篇关于如何递归遍历 PyGtk 中的所有 Gtk 子项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆