如何非交互式访问现有 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接口(interface)的,而不是关于一些用户界面。 - 回到过去的美好时光,我们可以使用
dcop
对 QT 应用程序执行类似的操作,但它当然不适用于 GTK。
最佳答案
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
的文档字符串中以及该目录中的其他文件。
关于widget - 远程控制/检查现有 GTK 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65190543/