0

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 itself (the first pop-up) again.

How can I run the other_python_file and pop-up 'itself' at the same time — so I can be able to trigger many functions before each one gets completed?

import sys, os
from tkinter import *
import tkinter as tk

root = Tk()

def Sample_Function():
    root.destroy()
    sys.path.insert(0,'C:/Data')
    import other_python_file
    os.system('python this_tk_popup.py')

tk.Button(text='Run Sample_Function', command=Sample_Function).pack(fill=tk.X)
tk.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
guialmachado
  • 506
  • 5
  • 17
  • How is your code not working? – martineau Nov 07 '20 at 01:17
  • I want it to open os.system('python this_tk_popup.py') while it is runing the opther_python_file. For now it is waiting for opther_python_file to conclude runing to run this_tk_popup.py – guialmachado Nov 07 '20 at 02:05
  • Try replacing `root.destory()` with `root.quit()`. – martineau Nov 07 '20 at 02:08
  • Still not runing 'this_tk_popup' while runing 'other_python_file'. But thistime, the 'first_tk_popup' is still running but not responding – guialmachado Nov 07 '20 at 02:33
  • The `root.quit()` should cause the currently running `mainloop()` to be exited. You may have to do the `root.destory()` first to make its window disappear if it doesn't automatically. The `import other_python_file` isn't quite the same thing as running the script, and without knowing what it does it's to difficult to predict whether the following `os.system()` call will get executed or not. – martineau Nov 07 '20 at 08:13
  • Can you just call `os.system('python other_python_file.py')` inside `simple_Function()`? – acw1668 Nov 07 '20 at 11:53
  • @martineau other_python_file is a webscraping program that takes 30 seconds. It is running the program but the tk popup takes 30s reappear. If I change the order, the webscraping program runs after o close the tk popup – guialmachado Nov 07 '20 at 18:46

1 Answers1

2

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

def sample_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)
martineau
  • 119,623
  • 25
  • 170
  • 301