python无条件跳转_如何基于按钮点击无条件地重新运行python程序?

我建议您使用subprocess模块重新执行该程序,该程序旨在替换旧的os.exec...()组函数。在

下面是一个可运行(即完整)的示例,说明如何使用它重新启动脚本,该脚本在Windows上用Python 3.6.4进行了测试:import os

import subprocess

import sys

import tkinter as tk

import traceback

class Application(tk.Frame):

def __init__(self, master=None):

tk.Frame.__init__(self, master)

self.pack(fill="none", expand=True) # Center the button.

self.create_widgets()

def create_widgets(self):

self.restart_btn = tk.Button(self, text='Restart', command=self.restart)

self.restart_btn.grid()

def restart(self):

command = '"{}" "{}" "{}"'.format(

sys.executable, # Python interpreter

__file__, # argv[0] - this file

os.path.basename(__file__), # argv[1] - this file without path

)

try:

subprocess.Popen(command)

except Exception:

traceback.print_exc()

sys.exit('fatal error occurred rerunning script')

else:

self.quit()

app = Application()

app.master.title('Restartable application')

app.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值