问题
I have main thread that runs a WX Python GUI. The main GUI gives a user a graphical interface to select scripts (which are mapped to python functions) and a 'Start' button, which spawns a second thread (Thread #2) to execute the selected scripts. The problem I am having is I cannot get a new WX GUI (GUI2) to popup and give the user the ability to enter data.
# Function that gets invoked by Thread #2
def scriptFunction():
# Code to instantiate GUI2; GUI2 contains wx.TextCtrl fields and a 'Done' button;
# When 'Done' button is clicked, data entered in fields are process and second GUI is destroyed
gui2Frame = Gui2Frame(None, "Enter Data Gui")
gui2Frame.Show(True)
# This is where I need help. Cannot figure out how to pend for user input;
# In this example; my 'Done' button sets the Event flag
verifyEvent = threading.Event()
verifyEvent.wait(10)
# Process entered data time
processData()
Currently, this approach locks up the GUI2 for 10sec. Which makes sense, because the spawned thread is locked up. Implementing a while-loop with a time.sleep(), does the same. I looked into spawning a third thread, just to handle GUI2, but again, I get back into not knowing how to hold GUI2 active. Any suggestions? Also, please no recommendations about changing the multithreading architecture into one thread; Thank you.
回答1:
You can't have two wxPython mainloops in one program. You really do have to use just the first wxPython program's main thread. If you want to spawn another application, then use subprocess. Something like this should work:
import subprocess
subprocess.Popen("python path/to/myscript.py")
来源:https://stackoverflow.com/questions/10679134/instantiating-a-new-wx-python-gui-from-spawn-thread