如何解决你能用一个命令来制作一个按钮来在 TkInter 中重新运行你的代码吗?
假设我想制作一个 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()
如果您有任何问题,请随时在此处发表评论。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。