【问题标题】:How to check if window exists in Python3.x on Linux如何在 Linux 上的 Python3.x 中检查窗口是否存在
【发布时间】:2015-11-18 23:46:02
【问题描述】:

我目前正在尝试弄清楚如何在 Linux 上通过 Python 访问一种“linux api”。我的问题是我一直想在 Windows 上以 autoit 方式做事,所以我不仅要掌握新语言,还要掌握操作系统。

基本上我的代码是这样的:

while (1)
if winexists("windowname") = 1 then
  kill(pid)
endif
wend

我可以通过 Windows API 或直接通过 autoit 执行此操作,但我不确定如何在 linux 中解决此问题。而且我在谷歌上没有找到太多相关的搜索结果。我不是在找人为我编码,只需要指出正确的方向。

【问题讨论】:

    标签: python linux windows api python-3.x


    【解决方案1】:

    您可以使用subprocess module 来使用外部程序(如wmctrl)的输出:

    import subprocess
    
    def winexists(target):
        for line in subprocess.check_output(['wmctrl', '-l']).splitlines():
            window_name = line.split(None, 3)[-1].decode()
            if window_name == target:
                return True
        return False
    

    Related question in superuser.com - Get a list of open windows in Linux

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2011-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多