从 class 实例获取属性而不传递 Self

Get attribute from class instance without passing Self

from pynput.mouse import Listener, Button

class ClickListener:
    def __init__(self):
        self.ClickTimes = 0
        self.CurrentClick = 0
        self.ClickPositions = []

    def on_click(x, y, button, pressed):
        if pressed and button == Button.middle:
            ClickPositions.append((x,y))
            self.CurrentClick += 1
            if self.ClickTimes <= self.CurrentClick:
                return Falses

    def getClicks(self, nr):
        self.ClickPositions = []
        self.CurrentClick = 0
        self.ClickTimes = nr
        with Listener(on_click = ClickListener.on_click) as listener:
            listener.join()
        return self.ClickPositions

关于函数 'on_click' 我怎样才能访问 self 属性,因为是调用函数的事件?看来我无法将 self 对象传递给事件。

编辑 1.:

class ClickListener:
    def __init__(self):
        self.ClickTimes = 0
        self.CurrentClick = 0
        self.ClickPositions = []

    def on_click(self, x, y, button, pressed):
        if pressed and button == Button.middle:
            self.ClickPositions.append((x,y))
            self.CurrentClick += 1
            if self.ClickTimes <= self.CurrentClick:
                return Falses

    def getClicks(self, nr):
        self.ClickPositions = []
        self.CurrentClick = 0
        self.ClickTimes = nr
        with Listener(on_click = self.on_click) as listener:
            listener.join()
        return self.ClickPositions

在这种情况下,它表示缺少一个必需的参数,'pressed' 一个。

你可以使用类似这样的东西。

from pynput.mouse import Listener, Button

class ClickListener:
    def __init__(self):
        self.ClickTimes = 0
        self.CurrentClick = 0
        self.ClickPositions = []

    @classmethod
    def on_click(cls, x, y, button, pressed):
        if pressed and button == Button.middle:
            cls.ClickPositions.append((x,y))
            cls.CurrentClick += 1
            if cls.ClickTimes <= cls.CurrentClick:
                return Falses

    @classmethod
    def getClicks(cls, nr):
       cls.ClickPositions = []
       cls.CurrentClick = 0
       cls.ClickTimes = nr
       with Listener(on_click = cls.on_click) as listener:
            listener.join()
       return cls.ClickPositions