从 spawn 线程实例化一个新的 WX Python GUI [英] Instantiating a new WX Python GUI from spawn thread
问题描述
我有一个运行 WX Python GUI 的主线程.主 GUI 为用户提供了一个图形界面来选择脚本(映射到 python 函数)和一个开始"按钮,它产生第二个线程(线程 #2)来执行选定的脚本.我遇到的问题是我无法弹出新的 WX GUI (GUI2) 并让用户能够输入数据.
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()
目前,这种方法会将 GUI2 锁定 10 秒.这是有道理的,因为生成的线程被锁定.使用 time.sleep() 实现一个 while 循环,也是如此.我考虑生成第三个线程,只是为了处理 GUI2,但是我又回到不知道如何保持 GUI2 处于活动状态.有什么建议么?另外,请不要建议将多线程架构更改为一个线程;谢谢.
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.
推荐答案
一个程序中不能有两个 wxPython 主循环.您确实必须只使用第一个 wxPython 程序的主线程.如果要生成另一个应用程序,请使用子进程.这样的事情应该可以工作:
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")
这篇关于从 spawn 线程实例化一个新的 WX Python GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!