【问题标题】:Remote control / inspect existing GTK application远程控制/检查现有 GTK 应用程序
【发布时间】:2020-12-07 22:44:57
【问题描述】:

我如何以非交互方式访问现有 GTK 应用程序的小部件树?我想查询一个小部件的坐标,如果可能的话发送事件(悬停、点击、按键……)。

使用 GTKInspector,我可以交互地浏览小部件树并查看小部件的大小和坐标。我想使用命令行工具或使用任何语言的 GTK API 访问此信息。

# Steps to do this interactively, I'd like a non-interactive version.
sudo apt install libgtk-3-dev
gsettings set org.gtk.Settings.Debug enable-inspector-keybinding true
GTK_DEBUG=interactive mate-calc

# Then go to `MathWindow → GtkBox → GtkMenuBar`
# click on the (i) icon to show the details view
# `allocation` indicates the `width×height+x+y`, here it's `312×25+0+0`

我考虑过的解决方案:

  • 使用 xdotool 自动控制 GTKInspector(预计性能不佳 + 需要额外的 GTKInspector 窗口)。
  • 使用 dbus,但 d-feet 不显示小部件树,我怀疑它显示的关于 interface 的内容是关于 OOP 或 d-bus 接口,而不是关于某些 用户 接口。
  • 在过去的美好时光中,我们可以使用 dcop 为 QT 应用程序做类似的事情,但它当然不适用于 GTK。

【问题讨论】:

    标签: widget gtk ui-automation gtk3 gtk2


    【解决方案1】:

    dogtail 使用辅助技术来访问应用程序的小部件树。在 Xubuntu 20.04 上测试。

    $ sudo apt install python3-dogtail
    $ sniff # explore using a GUI, it will enable assistive technologies for you.
    

    dogtail-example.py:

    #!/usr/bin/env python3
    from dogtail.tree import *
    app = root.application('mate-calc')
    menuBar = app.findChildren(predicate.GenericPredicate(roleName="menu bar"))[0]
    print('On-screen coordinates: ', menuBar.position)
    print('Size:                  ', menuBar.size)
    # to preview the nodes available in the menu bar, use menuBar.dump()
    

    输出:

    $ python3 dogtail-example.py
    On-screen coordinates: (529, 431)
    Size:                  (312, 25)
    

    /usr/share/doc/python3-dogtail/examples/ 中有一些示例,但实际的“doc”在/usr/lib/python3/dist-packages/dogtail/tree.py 的文档字符串和该目录中的其他文件中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-30
      • 2011-12-12
      • 1970-01-01
      • 2015-05-22
      • 1970-01-01
      • 2015-04-03
      • 2011-04-05
      相关资源
      最近更新 更多