有没有办法绕过在参数中引用 self 的需要Python

问题描述

我的代码的目的是创建和打印发生的所有键盘和鼠标交互的列表。该代码正在运行,但后来我决定将其转换为一个类。从那里,我无法引用这些函数,因为它认为 self 是第一个参数。

这是不工作的类的代码

import pyautogui
from pynput.mouse import Listener
MouseListener = Listener
from pynput.keyboard import Listener
KeyboardListener = Listener
from pynput import keyboard

######################
#
# m = move
# p = mouse press
# r = mouse release
# d = scroll down
# u = scroll up
# 1 = key down
# 2 = key up
#
######################

temp = []
class Recorder():
    #record mouse movement
    def on_move(self,x,y):
        temp.append('m{0}'.format(
            (x,y)))

    #record mouse click
    def on_click(self,y,button,pressed):
        temp.append('{0}{1}'.format(
            'p' if pressed else 'r',(x,y)))

    #record mouse scroll
    def on_scroll(self,dx,dy):
        temp.append('{0}{1}'.format(
            'd' if dy < 0 else 'u',y)))

    #record keyboard press
    def on_press(self,key):
        try:
            temp.append('1({0})'.format(
                key.char))
        except AttributeError:
            temp.append('1({0})'.format(
                key))

    #record keyboard release and end if esc
    def on_release(self,key):
        temp.append('2({0})'.format(
            key))
        if key == keyboard.Key.esc:
            # Stop listener
            print(temp)
            keyboard_listener.stop()
            mouse_listener.stop()
            return False

mouse_listener = MouseListener(
    on_move=Recorder.on_move,on_click=Recorder.on_click,on_scroll=Recorder.on_scroll)

keyboard_listener = KeyboardListener(
    on_press=Recorder.on_press,on_release=Recorder.on_release)

keyboard_listener.start()
mouse_listener.start()
keyboard_listener.join()
mouse_listener.join()

下面是正确执行的代码

import pyautogui
from pynput.mouse import Listener
MouseListener = Listener
from pynput.keyboard import Listener
KeyboardListener = Listener
from pynput import keyboard

##########
#
# m = move
# p = mouse press
# r = mouse release
# d = scroll down
# u = scroll up
# 1 = key down
# 2 = key up
#
##########

temp = []

#mouse recorder
def on_move(x,y):
    temp.append('m{0}'.format(
        (x,y)))

def on_click(x,pressed):
    temp.append('{0}{1}'.format(
        'p' if pressed else 'r',y)))

def on_scroll(x,dy):
    temp.append('{0}{1}'.format(
        'd' if dy < 0 else 'u',y)))

#keyboard recorder
def on_press(key):
    try:
        temp.append('1({0})'.format(
            key.char))
    except AttributeError:
        temp.append('1({0})'.format(
            key))

def on_release(key):
    temp.append('2({0})'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        print(temp)
        keyboard_listener.stop()
        mouse_listener.stop()
        return False

#activation
mouse_listener = MouseListener(
    on_move=on_move,on_click=on_click,on_scroll=on_scroll)

keyboard_listener = KeyboardListener(
    on_press=on_press,on_release=on_release)

keyboard_listener.start()
mouse_listener.start()
keyboard_listener.join()
mouse_listener.join()

解决方法

您可以为此使用装饰器 @staticmethod。在相关函数上方添加 @staticmethod。例如:

class Recorder:
    #record mouse movement
    @staticmethod
    def on_move(x,y,temp):
        temp.append('m{0}'.format((x,y)))

您现在可以在不实例化对象的情况下使用该函数。 调用函数:

Recorder.on_move(x,temp)

Documentation