我的限制是我最大的敵人,但最好的學習夥伴。從另一個類調用一個類的功能
我有兩個類:
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"
我可以通過'高清startStop'爲'的WorkerThread(線程)'?我添加了上面啓動'WorkerThread'的事件,以查看清晰度是否有幫助。 – chow
你引發了我需要知道的東西 - 我在'__init__'聲明中添加了'func',並添加了一個全局變量並且工作正常。 :)更新,上面的工作代碼。 – chow
如果您將其設爲屬性,則不需要全局屬性。 'self.function'和'self.function()'。在'run'結尾處'return'也是不必要的。你可以合併你的兩個條件來避免'return None'。 '而工人,而不是self.ToKill' – Pythonista