【问题标题】:How to close a pop-up window from other application with python script如何使用 python 脚本关闭其他应用程序的弹出窗口
【发布时间】:2019-04-25 11:33:51
【问题描述】:

我正在自动化 Aspen Plus 模拟和使用 Python 对结果进行后处理。为了进行放大分析、敏感性研究和解决优化问题;我必须迭代几次运行 aspen 模拟。

这样,我使用 win32com.client 来管理它。它工作得很好,但有时 aspen 会显示一个弹出窗口,告知所有许可证都在使用中,从而中断程序流程:

如果我手动关闭它,程序会继续工作。所以我正在考虑编写一个脚本来自动执行此操作,但我不知道如何做到这一点。

我试图杀死、终止、向进程发送信号。但没有任何效果,因为杀死 AspenPlus.exe 进程,停止程序。

【问题讨论】:

    标签: python aspen


    【解决方案1】:

    这里是一些示例 Powershell 代码,您可以使用它来检查 Aspen 进程是否正在运行以及窗口是否打开,然后通过 sendKey 函数发送适当的密钥来关闭它

    $isAspenOpen = Get-Process Aspen*
    if($isAspenOpen = $null){
        # Aspen is already closed run code here:
        }
    else {
         $isAspenOpen = Get-Process AspenPlus*
    
         # while loop makes sure all Aspen windows are closed before moving on to other code:
             while($isAspenOpen -ne $null){
                Get-Process aspen* | ForEach-Object {$_.CloseMainWindow() | Out-Null }
                sleep 5
                If(($isAspenOpen = Get-Process aspen*) -ne $null){
                Write-Host "Aspen is Open.......Closing Aspen"
                    $wshell = new-object -com wscript.shell
                    $wshell.AppActivate("Aspen Plus")
                    $wshell.Sendkeys("%(Esc)")
                $isAspenOpen = Get-Process Aspen*
                }
            }
            #Aspen has been closed run code here:
        }
    

    【讨论】:

      【解决方案2】:

      感谢 Psychon 解决方案!有用!!我从 python 中使用它,如下所示:

      import win32com.client as win32
      
      shell = win32.Dispatch("WScript.Shell")
      shell.AppActivate("All licenses are in use")
      shell.Sendkeys("%{F4}", 0)
      

      现在我只需要改进代码来自动完成任务。

      【讨论】:

      • 如果您想自动执行此任务,您可以为 Windows 创建一个 cronjob 等效项,称为计划任务。您将能够选择您希望计划任务执行的日期、时间等。我假设这是您所追求的那种自动化?
      • 是的。我做了一个简单的脚本,它检查窗口是否每 30 秒打开一次,如果是,请关闭它。它运作良好!现在我可以继续运行我的模拟并且睡得很好:D
      猜你喜欢
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-21
      相关资源
      最近更新 更多