Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 你能用命令制作一个按钮,在TkInter中重新运行你的代码吗?_Python_Tkinter_Widget - Fatal编程技术网

Python 你能用命令制作一个按钮,在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

假设我想制作一个TkInter按钮,重新启动或重新运行整个代码,例如:

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()

如果您有任何问题,请随时在此处发表评论。

它停止程序,但实际上不会重新运行它,尽管它停止程序,但实际上不会重新运行它