我的 GUI 使用 wxPython。在里面AppLogic
类我有一个工作线程,它在这个类的方法中运行。
这是GUI
class:
class GUI:
_wx_app = None
_main_window = None
_app_logic = None
def start(self):
# bla bla bla
self._main_window.Show()
self._app_logic.begin()
self._wx_app.MainLoop()
def _cancel_listener(self):
"""Called from MainWindow, if the user has clicked the cancel
button."""
print("I'm leaving this scope.")
self._app_logic.cancel() # Should cancel the task done in
# a second thread.
def _status_listener(self, status_text):
"""Called often by the worker thread."""
print("Here I am again.")
if self._main_window.status.GetLabel() != status_text:
self._main_window.status.SetLabel(status_text)
这是来自的取消方法AppLogic
被调用的类_cancel_listener
从上面:
def cancel(self):
self._shall_abort = True
self._thread.join(self._some_time_out)
assert self._thread.isAlive() == False
不知何故,出现了僵局join
and GetLabel
(因此MainLoop
?)参与,但我真的不明白发生了什么。有人对此有更多的见解吗?那太好了!
所有 GUI 工具包都有一个主 GUI 线程。它们都有特殊的方法,允许您以线程安全的方式操作 GUI 小部件。在wxPython世界中,这些方法是wx.CallAfter、wx.CallLater和wx.PostEvent。我在你的示例中没有看到这些,所以你所做的基本上是暂停 GUI 线程或导致发生“未定义”的情况。
这里有几篇关于线程和 wxPython 的文章:
- http://wiki.wxpython.org/LongRunningTasks http://wiki.wxpython.org/LongRunningTasks
- http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/ http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)