我的局限是我最大的敌人,但最重要的是学习伙伴。
我有两个班级:
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
和
class VolumePanel(wx.Panel):
#<...snip...>
def launchVolClick(self, event):
if self.bt_Launch.Label == "Enable Monitor":
self.worker = WorkerThread(self)
self.bt_Launch.Label = "Disable Monitor"
else:
self.worker.ToKill = True
self.bt_Launch.Label = "Enable Monitor"
def startStop(self):
print "in def startStop()"
我想找到一种从startStop
内拨打WorkerThread
的方法。我已经尝试了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"
答案 0 :(得分:1)
您可以传递对startStop
的引用,并从线程类中调用引用作为一个选项。如果没有看到更多的代码/代码的结构,很难说其他选项。
这是前者的一个人为举例。您不必以这种方式传递信息,您可以在VolumePanel
中调用该帖子并传递self.startStop
。
此外,worker
未定义,proc
也是如此,除非这是wxpython
的某些部分,我不熟悉。
from threading import Thread
class WorkerThread(Thread):
def __init__(self, func):
Thread.__init__(self)
self.func = func
def run(self):
for i in range(10):
self.func()
class VolumePanel:
def __init__(self):
#self.thread = WorkerThread(self.startStop)
#self.thread.start() #or elsewhere
pass
def startStop(self):
print "in def startStop()"
vp = VolumePanel()
thread = WorkerThread(vp.startStop)
thread.start()
答案 1 :(得分:0)
您也可以将调用类传递给线程,如下所示:
express()
请注意,我们使用class WorkerThread(threading.Thread):
def __init__(self, parent):
super(WorkerThread, self).__init__()
self.parent = parent
def run(self):
for i in range(10):
wx.CallAfter(self.parent.startStop)
class VolumePanel(object):
def startStop(self):
print "in def startStop()"
而不是直接调用函数。这是因为如果我们直接调用它,它实际上是从线程而不是wx.CallAfter()
调用的。这有时可能是一个问题,取决于你正在做什么。
如果我们在没有MainThread
的情况下打印出当前主题(threading.current_thread()
),我们就会
wx.CallAfter
然而,对于<WorkerThread(Thread-1, started 6612)>
<WorkerThread(Thread-1, started 6612)>
<WorkerThread(Thread-1, started 6612)>
<WorkerThread(Thread-1, started 6612)>
<WorkerThread(Thread-1, started 6612)>
<WorkerThread(Thread-1, started 6612)>
<WorkerThread(Thread-1, started 6612)>
<WorkerThread(Thread-1, started 6612)>
<WorkerThread(Thread-1, started 6612)>
<WorkerThread(Thread-1, started 6612)>
,我们得到了
wx.CallAfter