flet
flet copied to clipboard
closing methods don't work
Duplicate Check
- [x] I have searched the opened issues and there are no duplicates
Describe the bug
I was making a simple screenshot program for myself using flet but when added a quit button it didn't function. The button never works. I have tried
- page.close()
- page.window_destroy()
- page.window_close()
- os._exit(0)
- sys.exit() etc.
Nothing works. why? And can anyone tell me a way to fix it?
Code sample
This is the code.
import flet as ft
import mss
import os
from datetime import datetime
import keyboard
import threading
def main(page: ft.Page):
page.title = "Screenshot App"
page.bgcolor = "#2e2e2e"
page.window_width = 100
page.window_height = 150
save_dir = os.path.join(os.path.expanduser("~"), "Pictures", "Screenshots", "DEV")
os.makedirs(save_dir, exist_ok=True)
def take_screenshot(e=None):
with mss.mss() as sct:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
file_name = f"screenshot_{timestamp}.png"
file_path = os.path.join(save_dir, file_name)
sct.shot(output=file_path)
status_text.value = f"Screenshot saved: {file_path}"
page.update()
def stop_process(e=None):
page.window_destroy()
status_text = ft.Text("Press 'Ctrl+Shift+S' to take a screenshot.", color="white")
stop_button = ft.ElevatedButton("Stop Process", on_click=stop_process, bgcolor="#444444", color="black")
page.add(
status_text,
stop_button,
)
def hotkey_thread():
keyboard.add_hotkey('ctrl+shift+s', take_screenshot)
keyboard.add_hotkey('ctrl+shift+q', lambda _: page.window_destroy())
keyboard.wait()
threading.Thread(target=hotkey_thread, daemon=True).start()
ft.app(target=main)
To reproduce
Just run it in an IDE in Windows using .venv on python 3.13.2
Expected behavior
def stop_process(e=None):
page.window_destroy()
status_text = ft.Text("Press 'Ctrl+Shift+S' to take a screenshot.", color="white")
stop_button = ft.ElevatedButton("Stop Process", on_click=stop_process, bgcolor="#444444", color="black")
This part should close the app. When I press on the stop process button.
Screenshots / Videos
Captures
[Upload media here]
Operating System
Windows
Operating system details
WIndows 10
Flet version
0.27.6
Regression
I'm not sure / I don't know
Suggestions
No response
Logs
Logs
[Paste your logs here]
Additional details
No response
page.window.destroy()
https://flet.dev/docs/reference/types/window