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
9.43k stars 360 forks source link

how to restart application after packaged as exe #3174

Closed iorilu closed 2 weeks ago

iorilu commented 2 weeks ago

for example i need to restart the application to reload some config file how to do this , like click a button to restart itself

i tried some code , use os.execv from linke below, without packaed , the code works fine https://stackoverflow.com/questions/72868924/restart-compiled-python-script-with-pyinstaller

the problem is , the application create a new window, but old window still exists , so now there are two application windows, i need only one window, it seems old window can't close by itself, even i use page.window_close() here is sample code

import flet as ft
import os
import time
import sys

def main(page: ft.Page):

    def restart(e):
        # Restart the application...
        executable = sys.executable
        print(executable)
        executable_filename = os.path.split(executable)[1]
        page.window_close()
        if executable_filename.lower().startswith('python'):
            # application is running within a python interpreter
            python = executable
            os.execv(python, [python, ] + sys.argv)
        else:
            # application is running as a standalone executable
            os.execv(executable, sys.argv)
    print('start')
    lbl_time = ft.Text(time.ctime())
    btn_restart = ft.ElevatedButton('restart', on_click=restart)
    page.add(lbl_time, btn_restart)

ft.app(main)