I'm learning python and wxPython at the same time... :) So, I have a set of python files that run fine from the command line: ./scan -d test ~/Pictures
-- for instance will create a database of my pictures called "test."
I've been working diligently on a front end for this (I didn't write the original command line python files), and I can get it to run using:
def bt_ScanUpdateClick(self, event):
self.SetSizeWH(450,360)
## DEBUG
self.tc_MainDatabase.Value = "test.db"
if self.tc_MainDatabase.Value == "":
self.LogWindow.Value += "ERROR:\tNo database name selected!\n"
else:
scanCMD = "./scan -d " + self.tc_MainDatabase.Value + " "
numLines=0
maxLines=(int(self.multiText.GetNumberOfLines()))
if self.multiText.GetLineText(numLines) == "":
self.LogWindow.Value += "ERROR\tNo folder selected to scan!\n"
else:
self.LogWindow.Value += "Running Scan...\n\n"
while (numLines < maxLines):
scanCMD += str(self.multiText.GetLineText(numLines)) + " "
numLines += 1
self.LogWindow.Value += scanCMD
p = subprocess.Popen([scanCMD],shell=True,stdout=subprocess.PIPE,stdin=subprocess.PIPE)
self.LogWindow.Value += p.communicate()[0]
This presents a problem for me:
Any thoughts?
Roughly speaking, screen updates happen during each iteration of the event loop. While your code is running, the event loop is stuck in the same iteration and thus screen updates can't happen.
To work around this you need to run your long-running task in a separate thread or process. Because wxPython is single-threaded, any updates to the GUI (such as writing the lines of output) must be done in the main thread. To do that, your worker thread can use wx.CallAfter
which will schedule a command to be run in the GUI thread at its earliest convenience.
For more information see the page Long Running Tasks on the wxPython wiki, and see this answer to the question wxPython: Problems with thread. on this website.