Instantiating a new WX Python GUI from spawn thread

我的梦境 提交于 2020-01-06 15:48:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!