Feature request: Enable detection of window minimised state

Aperta
#93 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
39/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
desktop

Direzione di ricerca

Start by tracing the window.read and window.minimize entry points, then inspect how window.TKroot.state() and winfo_viewable() are exposed across supported platforms. The issue names no source files or tests; done should provide a reliable way to distinguish minimized, restored, and systray states so applications can respond accordingly.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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()
Lingua principale
Python
Stelle
867
Fork
106
Merge medio
1m
PR unite (30g)
2

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di spyoungtech/FreeSimpleGUI

Tutte le issue di spyoungtech/FreeSimpleGUI

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.