从另一个类调用类函数

2024-09-25 08:38:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我的局限性是我最大的敌人,也是最好的学习伙伴。在

我有两门课:

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"

Tags: selfnonetruereturninitdefnotifywindow
2条回答

您可以传递对startStop的引用,并从thread类中作为一个选项调用该引用。如果不了解更多的代码/代码的结构,很难说还有其他选项。在

这是前者的一个人为的例子。你不必用这种方式传递东西,你可以调用VolumePanel中的线程并传递self.startStop。在

而且,worker是未定义的,proc也是未定义的,除非这是{}中我不熟悉的部分。在

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

在 也可以将调用类传递给线程,如下所示:

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,我们得到

^{pr2}$

但是,使用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)>

相关问题 更多 >