Currently I program a GUI application with wxPython. Now I want create a STOP-Button which will stop the current python command/request/task.
I already created a button:
def StopButton(self, event):
sys.exit(0)
But it does not work. :( Because my program do not realize the click on the button. It does not react or respond because he is still busy with the current command/request/task.开发者_Go百科
With GUI applications, the UI runs on a single, often main, thread. This thread initializes the GUI elements, and waits for user input.
When you do other things in that main thread, your GUI elements cannot accept user input, because you're busy doing those other things. Sometimes this is what you want to have happen: right now, it's not.
Take a look at Python threading, and familiarize yourself with the concepts. What you want to do is, when you start your current command/request/task, start it in a new thread, so that when you continue to interact with your GUI, it can accept user input.
tl;dr: spawn a new thread for your c/r/t
All GUI applications run in their own thread. They're basically running in an infinite loop, waiting for the user to do "something" to which it can respond to. When you launch a long running process in the same thread, it blocks the GUI and keeps it from updating. To get around this, you run the long running process in another thread. See one of the following articles for more info:
http://wiki.wxpython.org/LongRunningTasks
http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
精彩评论