chow chow - 7 months ago 16
Python Question

Calling a Class function from another Class

My limitations are my worst enemy, but bestest learning buddy.

I have two classes:

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


And:

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()"


I want to find a way to call
startStop
from within the
WorkerThread
. I've tried this and couldn't get it to work either.

EDIT: Final working code below

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"

Answer

You could pass a reference to startStop and call the reference from within the thread class as one option. Without seeing more code / how your code is structured it's hard to say for other options.

Here's a contrived example of the former. You don't have to pass things around this way you could call the thread in VolumePanel and pass self.startStop.

Also, worker is undefined and so is proc unless this if some part of wxpython which I'm not familiar with.

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()