3

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.

tzman
  • 174
  • 1
  • 1
  • 11

3 Answers3

4

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

jkwhite
  • 320
  • 2
  • 10
0

Try with thys function, it may work for you.

app.kill()
Carlost
  • 478
  • 5
  • 13
0

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)
Bravhek
  • 327
  • 3
  • 10