tzman
tzman

Reputation: 174

How to close a window using pywinauto which does not have "File->exit" in menu?

I have performed some tasks on an application and now I want to close it's window but I don't want to kill the process (ie. it should keep running in system tray).

What's the correct way to do it in pywinauto? I am thinking about using pyautogui as a last resort.

NOTE: The application does not have a file menu.

Upvotes: 3

Views: 13245

Answers (3)

Bravhek
Bravhek

Reputation: 331

if you are over windows, you can use win32 library to close a window, here there a couple of function to help you do that if you know the window handle or the name.

from win32gui import FindWindow, PostMessage
import win32.lib.win32con as win32con


class WindowNotFound(Exception):
    ...


def find_window_by_name(window_name: str, class_name=None) -> int:
    """find a window by its class_name"""
    handle = FindWindow(class_name, window_name)
    if handle == 0:
        raise WindowNotFound(f"'{window_name}'")
    return handle


def close_window(handle: int):
    PostMessage(handle, win32con.WM_CLOSE, 0, 0)


handle = find_window_by_name("My Window Name")
close_window(handle)

Upvotes: 0

Carlost
Carlost

Reputation: 837

Try with thys function, it may work for you.

app.kill()

Upvotes: 0

jkwhite
jkwhite

Reputation: 320

You can check to see if there's a specific hotkey to close the program. However, the easiest way to do this is to send Alt-F4.

app.type_keys("%{F4}") # Alt-F4

or

app.send_keys("{VK_MENU}{F4}")

This is explained in the documentation

Upvotes: 4

Related Questions