Restart program tkinter

前端 未结 1 1634
Happy的楠姐
Happy的楠姐 2021-01-03 02:33

I am wondering on how I can create a restart button that once clicked, can restart the entire script. What I thought was that you destroy the window then un-destroy it but a

1条回答
  •  耶瑟儿~
    2021-01-03 03:19

    I found a way of doing it for a generic python program on this website: https://www.daniweb.com/programming/software-development/code/260268/restart-your-python-program. I wrote an example with a basic tkinter GUI to test it:

    import sys
    import os
    from tkinter import Tk, Label, Button
    
    def restart_program():
        """Restarts the current program.
        Note: this function does not return. Any cleanup action (like
        saving data) must be done before calling this function."""
        python = sys.executable
        os.execl(python, python, * sys.argv)
    
    root = Tk()
    
    Label(root, text="Hello World!").pack()
    Button(root, text="Restart", command=restart_program).pack()
    
    root.mainloop()
    

    0 讨论(0)
提交回复
热议问题