从另一个类调用一个类的功能
问题描述:
我的限制是我最大的敌人,但最好的学习伙伴。从另一个类调用一个类的功能
我有两个类:
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
并从线程类中调用的引用作为一个选项。没有看到更多的代码/你的代码的结构如何,很难说其他选项。
这是前者的一个人为的例子。你不必通过这种方式传递信息,你可以在VolumePanel
中调用线程并通过self.startStop
。
另外,worker
是undefined,所以是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()
答
您也可以passs调用类的线程像这样:
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
,我们得到
<_MainThread(MainThread, started 6604)>
<_MainThread(MainThread, started 6604)>
<_MainThread(MainThread, started 6604)>
<_MainThread(MainThread, started 6604)>
<_MainThread(MainThread, started 6604)>
<_MainThread(MainThread, started 6604)>
<_MainThread(MainThread, started 6604)>
<_MainThread(MainThread, started 6604)>
<_MainThread(MainThread, started 6604)>
<_MainThread(MainThread, started 6604)>
我可以通过'高清startStop'为'的WorkerThread(线程)'?我添加了上面启动'WorkerThread'的事件,以查看清晰度是否有帮助。 – chow
你引发了我需要知道的东西 - 我在'__init__'声明中添加了'func',并添加了一个全局变量并且工作正常。 :)更新,上面的工作代码。 – chow
如果您将其设为属性,则不需要全局属性。 'self.function'和'self.function()'。在'run'结尾处'return'也是不必要的。你可以合并你的两个条件来避免'return None'。 '而工人,而不是self.ToKill' – Pythonista