【问题标题】:pygtk get widget by name from parent nodepygtk 从父节点按名称获取小部件
【发布时间】:2012-03-15 15:33:31
【问题描述】:

我有一个 gtk 小部件,我想知道它的后代中是否还有另一个小部件。如果有,我想返回它,否则返回无。这是一个简单的递归问题,但我似乎无法找到正确的方法来做到这一点。

在林间空地 xml 文件中,我有:

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

并且对find(my_monkey_object, 'ook') 的调用应该返回 GtkTreeView 对象。 find() 应该类似于

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

我不确定我需要使用哪种 XXX() 方法。 get_name() 看起来很有希望,但返回的是对象的类名,而不是它的“id”。我使用的版本是 pygtk-2.24。

请参阅此Python GTK+ widget name 问题以了解相同的问题。

请注意,bug 可以解释问题:我希望生成器 ID 来自 GTK 小部件树。可悲的是,这似乎不可能得到......

【问题讨论】:

  • 我不确定它在 python 中是如何工作的,但是根据 gtk 文档,您可以从 gtk_buildable_get_name 获取“id”名称。
  • gtk.Buildable(widget).get_name() 在该错误的第 19 条评论中不起作用吗?

标签: python gtk pygtk


【解决方案1】:

根据 gtk C-api 文档,您可以像这样获取 glade "id" 名称:

name = gtk_buildable_get_name (GTK_BUILDABLE (widget))

对于pygtk,这与

相同
name = gtk.Buildable.get_name(widget)

【讨论】:

  • @ergosys @Sardathrion 所以name 是一个指针?而widget 是小部件名称?
【解决方案2】:

我猜你的节点对象是一个 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')

【讨论】:

  • 您的假设确实是正确的:node 是一个 gtk.Container 派生类。 isinstance() 方法并不理想,因为它将匹配所有 gtl.TreeView 而不仅仅是名为“ook”的那个。
【解决方案3】:

This answer 有。

适配使用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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    相关资源
    最近更新 更多