问题描述
假设我想制作一个 TkInter 按钮来重新启动或重新运行整个代码,例如:
restart_button = tk.Button(root,text = "Re-Run",command = whatever the code is or function to restart)
解决方法
尝试使用此代码:
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()
如果您有任何问题,请随时在此处发表评论。