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 apparently there is no un-destroy function.
            Asked
            
        
        
            Active
            
        
            Viewed 3.8k times
        
    7
            
            
        - 
                    Seems like a dupe of http://stackoverflow.com/q/731887/1072229 – Grisha Levit Jan 15 '17 at 00:20
- 
                    3Possible duplicate of [Resetting the main GUI window](http://stackoverflow.com/questions/731887/resetting-the-main-gui-window) – Grisha Levit Jan 15 '17 at 00:20
3 Answers
18
            
            
        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()
 
    
    
        j_4321
        
- 15,431
- 3
- 34
- 61
1
            
            
        I would Like to Use this Function:-
First of All Import os Module
import os
Then Use this Code:-
# Restarts the Whole Window    
def restart():
    root.destroy()
    os.startfile("main.py")
Or if You want no console behind then Simply Change the extension of the file to .pyw
And Run this Code:-
# Restarts the Whole Window    
def restart():
    root.destroy()
    os.startfile("main.pyw")
 
    
    
        MacStock Tech  Gaming
        
- 11
- 1
1
            
            
        The following solution works as well but is quite harsh, i.e. the entire environment is lost.
# kills the whole application and starts a fresh one
def restart():
    root.destroy()
    root = Tk()
    root.mainloop()
 
    
    
        ATH
        
- 666
- 6
- 13
