Skip to content Skip to sidebar Skip to footer

How To Run Two Parallel Scripts From Tkinter?

With this code I was able to create a TK Inter pop-up with a button to run a Sample_Function. This Sample_Function destroys the tk pop-up, runs another python file, and then opens

Solution 1:

I think this will do close to what you want. It uses subprocess.Popen() instead of os.system() to run the other script and rerun the pop-up which doesn't block execution while waiting for them to complete, so they can now execute concurrently.

I also added a Quit button to get out of the loop.

import subprocess
import sys
from tkinter import *
import tkinter as tk

root = Tk()

defsample_function():
    command = f'"{sys.executable}" "other_python_file.py"'
    subprocess.Popen(command)  # Run other script - doesn't wait for it to finish.
    root.quit()  # Make mainloop() return.

tk.Button(text='Run sample_function', command=sample_function).pack(fill=tk.X)
tk.Button(text='Quit', command=lambda: sys.exit(0)).pack(fill=tk.X)
tk.mainloop()
print('mainloop() returned')

print('restarting this script')
command = f'"{sys.executable}" "{__file__}"'
subprocess.Popen(command)

Post a Comment for "How To Run Two Parallel Scripts From Tkinter?"