1

首先,如果这个问题很愚蠢,我想道歉。

我有这个错误的问题:

    ImportError: cannot import name 'key' from 'pynput.keyboard' 
    (C:\Users\richard\AppData\Local\Programs\Python\Python37-32\lib\site- 
    packages\pynput\keyboard\__init__.py)

你能告诉我,如何解决它吗?:

我试图在谷歌上找到一些建议,但我没有找到任何东西。也许这只是太愚蠢的“问题”。

这是未完成的代码,我想尝试它是否有效,然后出现错误。

import pynput

from pynput.keyboard import key, Listener

count = 0
keys = []

def on_press(key):
    global keys, count
    print("{0} pressed".format(key))

def write_file(keys):
    with open("log.txt", "w") as f:
        for key in keys:
            f.write(key)

def on_realease(key):
    if key == Key.esc:
        return False

with Listener (on_press=on_press, on_release=on_realease) as listener:
    listener.join()

这是整个问题:

Traceback (most recent call last):
  File "C:/Users/richard/AppData/Local/Programs/Python/Python37- 
  32/Logger.py", line 3, in <module>
    from pynput.keyboard import key, Listener
ImportError: cannot import name 'key' from 'pynput.keyboard' 
(C:\Users\richard\AppData\Local\Programs\Python\Python37-32\lib\site- 
packages\pynput\keyboard\__init__.py)

Process finished with exit code 1
4

1 回答 1

4

Key不是key

from pynput.keyboard import Key, Listener

看看这里的文档。

于 2019-01-18T23:50:43.267 回答