重启程序 tkinter [英] Restart program tkinter

查看:37
本文介绍了重启程序 tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何创建一个重启按钮,一旦点击,就可以重启整个脚本.我的想法是你销毁窗口然后取消销毁它,但显然没有取消销毁功能.

I am wondering on how I can create a restart button that once clicked, can restart the entire script. What I thought was that you destroy the window then un-destroy it but apparently there is no un-destroy function.

推荐答案

我在这个网站上找到了一个通用 python 程序的方法:https://www.daniweb.com/programming/software-development/code/260268/restart-your-python-program.我用一个基本的 tkinter GUI 写了一个例子来测试它:

I found a way of doing it for a generic python program on this website: https://www.daniweb.com/programming/software-development/code/260268/restart-your-python-program. I wrote an example with a basic tkinter GUI to test it:

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

这篇关于重启程序 tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆