我的局限性是我最大的敌人,也是最好的学习伙伴。在
我有两门课:
class WorkerThread(Thread):
"""Worker Thread Class."""
def __init__(self, notify_window):
"""Init Worker Thread Class."""
Thread.__init__(self)
self._notify_window = notify_window
self.ToKill = False
self._want_abort = 0
self.start()
def run(self):
"""Run Worker Thread."""
while worker==True:
# somehow get to volumePanel.startStop()
if self.ToKill == True:
return None
proc.wait()
wx.PostEvent(self._notify_window, ResultEvent(None))
return
以及:
^{pr2}$我想找到一种从WorkerThread
中调用startStop
的方法。我试过this但也没能用。在
编辑:下面的最终工作代码
class WorkerThread(Thread):
"""Worker Thread Class."""
def __init__(self, notify_window, func):
"""Init Worker Thread Class."""
Thread.__init__(self)
self._notify_window = notify_window
self.ToKill = False
self._want_abort = 0
global function
function = func
self.start()
def run(self):
"""Run Worker Thread."""
while worker==True:
# somehow get to volumePanel.startStop()
function()
if self.ToKill == True:
return None
proc.wait()
wx.PostEvent(self._notify_window, ResultEvent(None))
return
def launchVolClick(self, event):
if self.bt_Launch.Label == "Enable Volume Monitor":
self.worker = WorkerThread(self, self.startStop)
self.bt_Launch.Label = "Disable Volume Monitor"
else:
self.worker.ToKill = True
self.bt_Launch.Label = "Enable Volume Monitor"
您可以传递对
startStop
的引用,并从thread类中作为一个选项调用该引用。如果不了解更多的代码/代码的结构,很难说还有其他选项。在这是前者的一个人为的例子。你不必用这种方式传递东西,你可以调用
VolumePanel
中的线程并传递self.startStop
。在而且,}中我不熟悉的部分。在
worker
是未定义的,proc
也是未定义的,除非这是{在 也可以将调用类传递给线程,如下所示:
注意,我们使用
wx.CallAfter()
,而不是直接调用函数。这是因为如果我们直接调用它,它实际上是从线程调用的,而不是MainThread
。这有时会成为一个问题,这取决于你在做什么。在如果我们打印出当前线程(带
^{pr2}$threading.current_thread()
)而没有wx.CallAfter
,我们得到但是,使用
wx.CallAfter
,我们得到相关问题 更多 >
编程相关推荐