Skip to content

Feature request: Enable detection of window minimised state #93

@nav9

Description

@nav9

It would help to have a feature that allows detecting whether the window is minimised or un-minimised or in systray. The reason it helps to have this feature is that the programmer can then write code to avoid having to process any GUI event loop when the program is minimised. In certain programs this can help save a huge amount of processing.
I tried asking an LLM if such a feature is already available, and it generated the below code but it does not detect the minimised and un-minimised state.

import platform

# Identify the OS
OS_TYPE = platform.system() # Returns 'Linux', 'Windows', or 'Darwin' (macOS)

layout = [
    [sg.Text("Window State Monitor", font=("Helvetica", 16))],
    [sg.Text("Current State:", size=(15, 1)), sg.Text("", key="-STATE-", text_color="yellow")],
    [sg.Button("Minimize Me"), sg.Button("Exit")]
]

window = sg.Window("OS State Demo", layout, finalize=True)

while True:
    # Use a short timeout to refresh the check periodically
    event, values = window.read(timeout=200)
    
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
    
    if event == "Minimize Me":
        window.minimize()

    # --- CROSS-PLATFORM STATE DETECTION ---
    
    # 1. Get the standard tkinter state
    raw_state = window.TKroot.state() 
    
    # 2. Check if the window is physically 'viewable' (especially useful on Linux)
    # returns 1 if visible on screen, 0 if minimized/hidden
    is_viewable = window.TKroot.winfo_viewable() 

    display_status = "Unknown"

    if OS_TYPE == "Windows":
        # Windows handles 'zoomed' and 'iconic' reliably
        if raw_state == "iconic":
            display_status = "MINIMIZED"
        elif raw_state == "zoomed":
            display_status = "MAXIMIZED"
        else:
            display_status = "VISIBLE (Normal)"

    elif OS_TYPE == "Linux":
        # On Linux, raw_state often stays 'normal'. Use winfo_viewable for accuracy.
        if is_viewable == 0 or raw_state == "iconic":
            display_status = "MINIMIZED (Iconified)"
        else:
            display_status = "VISIBLE (Normal)"
            
    elif OS_TYPE == "Darwin": # macOS
        if raw_state == "iconic":
            display_status = "MINIMIZED"
        else:
            display_status = "VISIBLE"

    # Update GUI and Console
    window["-STATE-"].update(display_status)
    print(f"[{OS_TYPE}] Raw State: {raw_state} | Viewable: {is_viewable} | Status: {display_status}")

window.close()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions