LiLMaNSerG
LiLMaNSerG

Reputation: 11

Need help interacting with Python GUI while there is a process being run

I am developing an application that will run a batch of a test when you press a Start Button on the GUI. The problem is that once that subprocess to run the test is called, the Python GUI freezes until the subprocess is finished executing. I am using Python 2.7 by the way.

I would like to interact with the GUI while the test is running, have different buttons that can be pressed, etc. without interrupting the test.

Here is an excerpt of what I have for this part:

import Tkinter
import tkMessageBox
import subprocess


top = Tkinter.Tk()

def batchStartCallBack():
    tkMessageBox.showinfo("Batch Application", "Batch STARTED!")
    for x in range(0, 3):
        p = subprocess.call('batch path', stdout = None, stderr = None, shell=False)

def batchStopCallBack():
    tkMessageBox.showinfo("Batch Application", "Batch Stopped!")
    # STOP BATCH

StartButton = Tkinter.Button(top, text = "Start Batch", command = batchStartCallBack, width = 8, height = 2)
StopButton = Tkinter.Button(top, text = "Stop Batch", command = batchStopCallBack, width = 8, height = 2)

StartButton.pack()
StopButton.pack()

top.mainloop()

Upvotes: 1

Views: 282

Answers (1)

Ed Smith
Ed Smith

Reputation: 13196

You should use subprocess.Popen which is non-blocking. Calls to subprocess.call will make the current script wait until the subprocess has finished. In a gui, an endless loop is run checking for input and this means your gui will unresponsive as you have seen. It may be possible to initialise a subprocess pool and use a separate subprocess for the gui and another for the run...

Upvotes: 1

Related Questions