How to use _get_window_handle method in pyatom

Best Python code snippet using pyatom_python

windows.py

Source:windows.py Github

copy

Full Screen

...56 super(WindowNotFoundError, self).__init__("Opus Magnum window not found. Is the game running?")57def _raise_error():58 error_code = ctypes.windll.kernel32.GetLastError()59 raise ScreenshotError(f"system call failed with error code {error_code:08x}")60def _get_window_handle():61 window_handle = ctypes.windll.user32.FindWindowA(NULL, ctypes.c_char_p(b"Opus Magnum"))62 if window_handle == NULL:63 raise WindowNotFoundError64 return window_handle65Dimensions = collections.namedtuple("Dimensions", "width height")66def _get_window_dimensions(window_handle):67 client_rect = RECT()68 result = ctypes.windll.user32.GetClientRect(window_handle, ctypes.byref(client_rect))69 if result == 0:70 _raise_error()71 return Dimensions(width=client_rect.right - client_rect.left, height=client_rect.bottom - client_rect.top)72def get_screenshot():73 window_handle = _get_window_handle()74 dimensions = _get_window_dimensions(window_handle)75 window_dc = ctypes.windll.user32.GetWindowDC(window_handle)76 bitmap_dc = ctypes.windll.gdi32.CreateCompatibleDC(window_dc)77 bitmap = ctypes.windll.gdi32.CreateCompatibleBitmap(window_dc, dimensions.width, dimensions.height)78 ctypes.windll.gdi32.SelectObject(bitmap_dc, bitmap)79 try:80 result = ctypes.windll.user32.PrintWindow(window_handle, bitmap_dc, PW_RENDERFULLCONTENT)81 if result == 0:82 raise ScreenshotError("PrintWindow failed")83 bmi = BITMAPINFO()84 ctypes.memset(ctypes.byref(bmi), 0x00, ctypes.sizeof(bmi))85 bmi.header.size = ctypes.sizeof(BITMAPINFOHEADER)86 bmi.header.width = dimensions.width87 bmi.header.height = dimensions.height88 bmi.header.planes = 189 bmi.header.bit_count = 2490 row_size = ((dimensions.width * bmi.header.bit_count + 31) // 32) * 491 buffer_size = row_size * dimensions.height92 buffer = ctypes.create_string_buffer(buffer_size)93 ctypes.windll.gdi32.GetDIBits(94 bitmap_dc, bitmap,95 0, dimensions.height,96 ctypes.byref(buffer),97 ctypes.byref(bmi),98 DIB_RGB_COLORS,99 )100 if result == 0 or result == ERROR_INVALID_PARAMETER:101 raise ScreenshotError("GetDIBits failed")102 return PIL.Image.frombuffer("RGB", dimensions, buffer, "raw", "BGR", row_size, -1)103 finally:104 ctypes.windll.gdi32.DeleteDC(bitmap_dc)105 ctypes.windll.gdi32.DeleteObject(bitmap)106 ctypes.windll.user32.ReleaseDC(window_handle, window_dc)107def set_window_foreground(handle=None):108 if handle is None:109 handle = _get_window_handle()110 ctypes.windll.user32.SetForegroundWindow(handle)111 time.sleep(.1)112def get_window_rectangle(handle=None):113 if handle is None:114 handle = _get_window_handle()115 rect = RECT()116 result = ctypes.windll.user32.GetWindowRect(handle, ctypes.byref(rect))117 if result == 0:118 _raise_error()119 return rect120def click_in_window(client_x, client_y):121 handle = _get_window_handle()122 set_window_foreground(handle)123 rect = get_window_rectangle(handle)124 x = client_x + rect.left125 y = client_y + rect.top126 pyautogui.mouseDown(button="left", x=x, y=y)127 time.sleep(0.1)128 pyautogui.mouseUp(button="left", x=x, y=y)129def click_new_game():130 set_window_foreground()131 center = pyautogui.locateCenterOnScreen("new_game_template.png")132 if not center:133 raise Exception("Couldn't find new game button, is Sigmar's Garden open?")134 x, y = center135 pyautogui.mouseDown(button="left", x=x, y=y)...

Full Screen

Full Screen

screenshot.py

Source:screenshot.py Github

copy

Full Screen

...10 def __init__(self, target: str, dpi: float=1.0) -> None:11 self._target = target12 self._dpi = dpi13 self._hwnd = None14 def _get_window_handle(self) -> int:15 toplist, winlist = [], []16 EnumWindows(lambda hwnd, _: winlist.append((hwnd, GetWindowText(hwnd))), toplist)17 windows = [(hwnd, title) for hwnd, title in winlist if self._target in title.lower()]18 19 if not windows:20 raise WindowNotFoundError(f'No window with name "{self._target}" found')21 22 return windows[0][0]23 def take_screenshot(self, save: bool=True) -> Image:24 if not self._hwnd:25 self._hwnd = self._get_window_handle()26 # SetForegroundWindow(self._hwnd)27 try:28 bbox = GetWindowRect(self._hwnd)29 except error:30 self._hwnd = self._get_window_handle()31 bbox = GetWindowRect(self._hwnd)32 33 bbox = tuple([n*self._dpi for n in bbox])34 img = ImageGrab.grab(bbox, all_screens=True)35 if save:36 img.save(f'screenshots/{time()}.png') 37 return img38if __name__ == '__main__':39 screenshot = Screenshot(target='league of legends', dpi=1.25)40 while True:41 try:42 screenshot.take_screenshot()43 sleep(10)44 except WindowNotFoundError as e:...

Full Screen

Full Screen

transparenter.py

Source:transparenter.py Github

copy

Full Screen

...8 def __init__(self, tk_window):9 self._tk_object = tk_window10 self._handle = None11 12 def _get_window_handle(self):13 if self._handle is None:14 self._handle = GetParent(self._tk_object.winfo_id())15 SetWindowLongA(self._handle, GWL_EXSTYLE, WS_EX_LAYERED)16 return self._handle17 18 def set_opacity(self, opacity):19 opacity = ct.c_ubyte(opacity)20 handle = self._get_window_handle()...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run pyatom automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful