切换到在 Python 上使用 pyautogui(alt 选项卡)以外打开的特定应用程序

问题描述 投票:0回答:1

我正在开发一个使用 pyautogui 的代码,我唯一需要的就是切换到一个已经打开的应用程序,而不是使用 alt + tab,例如:

import pyautogui

def start():

    #here comes the code that swap to the already oppened app, Paint.exe for example
    
    pyautogui.click(x, y) #click on a tool
    pyautogui.click(x, y) #select a colour
    pyautogui.click(x, y) #paint the background

这只是一个例子,我的代码要复杂得多,我使用

pyautogui.hotkey("alt", "tab")
作为解决方案,但应用程序始终需要是最后使用的应用程序,否则它不会切换到它,因此会导致有时会出错。

有谁知道这个问题的解决办法吗?

python automation pyautogui
1个回答
0
投票

我正在使用 pyautogui 和 ctypes 的组合来完成它。这是我尝试过的不同方法的大杂烩,但无法单独工作。我对这种编程很陌生,可能只用其中一种就可以做到。但这符合我的目的,而且我看到你没有得到任何更好的回应,所以......像这样:

windows=pyautogui.getAllWindows() 
hWnd = [x._hWnd for x in windows if x.title=='title of window to open']
user32 = ctypes.windll.user32
user32.SetForegroundWindow(hWnd)
if user32.IsIconic(hWnd): user32.ShowWindow(hWnd, 9)            
© www.soinside.com 2019 - 2024. All rights reserved.