flet-dev / flet

Flet enables developers to easily build realtime web, mobile and desktop apps in Python. No frontend experience required.
https://flet.dev
Apache License 2.0
11.15k stars 429 forks source link

Cannot trigger WindowEventType.CLOSE #4087

Open hionwi opened 1 week ago

hionwi commented 1 week ago

Duplicate Check

Describe the bug

I Cannot trigger WindowEventType.CLOSE when I close the windows

Code sample

Code ```python import flet as ft def main(page: ft.Page): page.add(ft.Text("Hello, World!")) page.window.on_event = lambda e: print(e) ft.app(main) ```

To reproduce

Run the code and close the window. The console does not print the WindowEventType.CLOSEinformation. But WindowEventType.MOVED,WindowEventType.BLUR,WindowEventType.FOCUS is OK

Expected behavior

No response

Screenshots / Videos

Captures [Upload media here]

Operating System

Windows

Operating system details

windows 11

Flet version

0.24.1

Regression

I'm not sure / I don't know

Suggestions

No response

Logs

Logs ```console [Paste your logs here] ```

Additional details

No response

ndonkoHenri commented 1 week ago

Issue is reproducible on windows, but not mac.

OwenMcDonnell commented 4 days ago

This issue seems to be a problem of synchronization of communication between Python and Flutter. It seems that there isn't time for the close event to be dispatched to the Python side before it closes.

You might try a different approach such as the example below.

import flet as ft

def main(page: ft.Page):

    def window_event(e):

        print(e)
        if e.data == "close":
            page.window.destroy()         

    page.window.prevent_close = True
    page.window.on_event = window_event

    page.add(ft.Text("Hello, World!"))

ft.app(main)