Python 你能用命令制作一个按钮,在TkInter中重新运行你的代码吗?
假设我想制作一个TkInter按钮,重新启动或重新运行整个代码,例如:Python 你能用命令制作一个按钮,在TkInter中重新运行你的代码吗?,python,tkinter,widget,Python,Tkinter,Widget,假设我想制作一个TkInter按钮,重新启动或重新运行整个代码,例如: restart\u button=tk.button(root,text=“Re Run”,command=重新启动的代码或函数) 我不想要任何复杂的东西,只要一个简单的函数或答案就可以了。尝试使用以下代码: import sys import os from tkinter import Tk, Label, Button def restart_program(): """Res
restart\u button=tk.button(root,text=“Re Run”,command=重新启动的代码或函数)
我不想要任何复杂的东西,只要一个简单的函数或答案就可以了。尝试使用以下代码:
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()
如果您有任何问题,请随时在此发表评论。尝试使用以下代码:
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()
如果您有任何问题,请随时在此处发表评论。它停止程序,但实际上不会重新运行它,尽管它停止程序,但实际上不会重新运行它