创建一个像 eclipse 一样的终止按钮
目前我使用 wxPython 编写一个 GUI 应用程序。 现在我想创建一个停止按钮来停止当前的 python 命令/请求/任务。
我已经创建了一个按钮:
def StopButton(self, event):
sys.exit(0)
但它不起作用。 :(因为我的程序没有实现按钮的点击。它没有反应或响应,因为他还在忙于当前的命令/请求/任务。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于 GUI 应用程序,UI 在单个线程(通常是主线程)上运行。该线程初始化 GUI 元素,并等待用户输入。
当您在该主线程中执行其他操作时,您的 GUI 元素无法接受用户输入,因为您正忙于执行其他操作。有时这就是您希望发生的事情:但现在却不是。
查看 Python 线程,熟悉这些概念。您想要做的是,当您启动当前的命令/请求/任务时,在新线程中启动它,以便当您继续与 GUI 交互时,它可以接受用户输入。
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.
所有 GUI 应用程序都在自己的线程中运行。它们基本上运行在无限循环中,等待用户做它可以响应的“某事”。当您在同一线程中启动长时间运行的进程时,它会阻止 GUI 并阻止其更新。为了解决这个问题,您可以在另一个线程中运行长时间运行的进程。有关详细信息,请参阅以下文章之一:
http://wiki.wxpython.org/LongRunningTasks
http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
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/