How to unconditionally re-run a python program based on button click?

Momo 6aye3

At the end of my program execution, I want a popup message to appear that has a button which can re-run a program. Obviously, I will have setup a function that the button calls when it is clicked, as such

def restart():
    **python command that makes the whole program restart**

Then I would attach this function to the following button:

B1 = ttk.Button(popup, text='Restart program', width=17, command=lambda: restart())

Is there such a command?

Quick note:I found an answer but it doesn't work, here it is:

os.execl(sys.executable, sys.executable, *sys.argv)
martineau

I suggest that you use the subprocess module to re-execute the program which was designed to replace the older os.exec...() group of functions.

Here's a runnable (i.e. complete) example of how to use it to restart the script, which was tested on Windows with 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()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to run python scripts on button click in React?

How to run a python program from powershell based on python hashbang?

How to run a python script with arguments via the click of a button in javascript

How to run a function on button click?

Goland run/re-run command from button click

How stop re-writing log data during program re-run / Python

How to run .exe file or .bat file based on button click event using Javascript

Program to run through functions in a single button (1 function = 1 click)

Click on button based on its Value in Selenium Python

How to run code on server on the click of button?

How to run application of the first with click on the button in android?

How to run code on a button click in android fragment

How to run methods in onCreate() without button click

How to run the Python program forever?

How to run python program as a daemon?

How to run a Python program directly?

How to script a button click based on Label or Identifier

How to click a button based on ref attribute?

How to update text based on button click?

How to populate table based on button click

How to add class in sequence based on button click?

How to webscrape and click a button based on user input

How to run a Python script upon button click using mod_python without changing browser window

How to click this button with python & selenium

Python - selenium - how to click on the button

How click in Button with Selenium python

How to run form action and click event simultaneously on button click

How to intercept button click, run analytics code, and then continue with click function

How to change the layout on button click based on the radio button?