GUI automation with python
Next, I want to start a project to create a bot for a specific Android game in python, for this, I use an emulator, nox. My idea is to be able to work with the automation part of gui (automatic clicks, pressing a keyboard button, etc.) in a way in the background, in case, that could leave the window minimized, and go performing other tasks.
The library I found pair to Gui automation python was PyAutoGUI, though you can only click the entire window if the pixels currently appear on the screen, which will not occur if the process is minimized
Is there any way I can do this process with the minimized window?
Follows example of a very simple code to give an exemplified
import os
import pygetwindow
import time
import pyautogui
title = "NoxPlayer"
os.startfile(r"C:\Program Files (x86)\Nox\bin\nox.exe")
time.sleep(5)
window = pygetwindow.getWindowsWithTitle(title)[0]
window.activate()
window.resizeTo(1280,720)
window.moveTo(0,0)
time.sleep(5)
posx, posy, height, wight = pyautogui.locateOnScreen(r"Images\Icon.png")
pyautogui.moveTo(posx + 10, posy + 10, 1)
1 answers
Is there any way I can do this process with the minimized window?
No. You will need to maximize the window so that image search methods (such as' locateonscreen') can be used.
To maximize a window, I usually use the function below. The 'win_name' argument must be the name of the process/window you want to maximize.
def win_act(win_name):
import win32gui
import re
class WindowMgr:
"""Encapsulates some calls to the winapi for window management"""
def __init__(self):
"""Constructor"""
self._handle = None
def find_window(self, class_name, window_name=None):
"""find a window by its class_name"""
self._handle = win32gui.FindWindow(class_name, window_name)
def _window_enum_callback(self, hwnd, wildcard):
"""Pass to win32gui.EnumWindows() to check all the opened windows"""
if re.match(wildcard,
str(win32gui.GetWindowText(hwnd))) is not None:
self._handle = hwnd
def find_window_wildcard(self, wildcard):
"""find a window whose title matches the wildcard regex"""
self._handle = None
win32gui.EnumWindows(self._window_enum_callback, wildcard)
def set_foreground(self):
"""put the window in the foreground"""
win32gui.SetForegroundWindow(self._handle)
w = WindowMgr()
for x in range(3):
w.find_window_wildcard(f".*{win_name}*")
# time.sleep(2)
w.set_foreground()