How to run a Python file on button click in tkinter?
Question:
I am having a Python page named Home.py and it is containing a button named View Reports. When I click this button I want another Python page named Reports.py to get executed. I developed the UI using tkinter.
Answers:
Try the below code. Works for me.
def RunWrapper():
wrapper = ['python', 'Reports.py']
result1 = subprocess.Popen(wrapper, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
out1, err1 = result1.communicate()
status_wrapper=out1.decode("utf-8")
tkinter.messagebox.showinfo("Execution of script is done")
Button.configure(pady="0",text='''Execute''',command=RunWrapper)
I am having a Python page named Home.py and it is containing a button named View Reports. When I click this button I want another Python page named Reports.py to get executed. I developed the UI using tkinter.
Try the below code. Works for me.
def RunWrapper():
wrapper = ['python', 'Reports.py']
result1 = subprocess.Popen(wrapper, stdin=subprocess.PIPE, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
out1, err1 = result1.communicate()
status_wrapper=out1.decode("utf-8")
tkinter.messagebox.showinfo("Execution of script is done")
Button.configure(pady="0",text='''Execute''',command=RunWrapper)