import tkinter as tk
import os, sys

class Desktop:
    def __init__(self, root):
        self.root = root
        self.restart_requested = False
        
    def save_and_quit(self):
        self.root.destroy()

root = tk.Tk()
desktop = Desktop(root)

def do_restart():
    desktop.restart_requested = True
    desktop.save_and_quit()

tk.Button(root, text="Restart", command=do_restart).pack()

root.after(1000, do_restart) # Auto trigger

print("Entering mainloop")
root.mainloop()
print(f"Exited mainloop. Restart requested: {desktop.restart_requested}")

if getattr(desktop, "restart_requested", False):
    print(f"Would exec: {sys.executable} {os.path.abspath(sys.argv[0])}")
