0

I am trying to create a GUI. I need to execute another python script while the GUI is active. (The GUI is supposed to process the data from this execution) Because I created the GUI using Tkinter, I am unable to execute another file in the python terminal.

How can I solve this problem?

Honest Abe
  • 8,430
  • 4
  • 49
  • 64
Jerin
  • 11
  • 1
  • 1

1 Answers1

1

You don't need to launch another interpreter.

You can simply execute the code from the other script in a separte thread or process.


Refactor your "other" script

You want your "other" script to be callable from another script. To do so, you'll just need a function that does what your script used to do.

#other.py

def main(arg1, arg2, arg3):
    do_stuff(arg1, arg2)
    more_stuff(arg2, arg3)
    other_stuff(arg1, arg3) 
    finish_stuff(arg1, arg2, arg3)

Execute the code in another thread

In your main script, when you want to execute the code from other.py, start a new thread:

 #script.py

 from threading import Thread

 from other import main

 thread = Thread(target = main)
 thread.start() # This code will execute in parallel to the current code

To check that whether your work is done, use thread.is_alive(). To block until it finishes, use thread.join()

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • Thanks Thomas. I got an answer similar to this from the following link, this is what i was looking for.. https://www.youtube.com/watch?v=TE7ysx3a7zU – Jerin Mar 13 '13 at 14:41