我想知道如何创建一个重新启动按钮,单击该按钮可以重新启动整个脚本。我以为您先破坏窗口然后销毁它,但是显然没有销毁功能。
我在以下网站上找到了用于通用python程序的方法:https : //www.daniweb.com/programming/software-development/code/260268/restart-your-python-program。我用基本的tkinter GUI编写了一个示例来对其进行测试:
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()
Run Code Online (Sandbox Code Playgroud)