在Python程序中获取“NotImplementedError”[已关闭]

问题描述 投票:0回答:1

错误:

C:\Users\wande\PycharmProjects\pythonProject\venv\Scripts\python.exe C:\Users\wande\PycharmProjects\pythonProject\main.py 
Unhandled exception in listener callback
Traceback (most recent call last):
  File "C:\Users\wande\PycharmProjects\pythonProject\venv\lib\site-packages\pynput\_util\win32.py", line 386, in _handler
    converted = self._convert(code, msg, lpdata)
  File "C:\Users\wande\PycharmProjects\pythonProject\venv\lib\site-packages\pynput\_util\win32.py", line 401, in _convert
    raise NotImplementedError()
NotImplementedError

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\wande\PycharmProjects\pythonProject\venv\lib\site-packages\pynput\_util\__init__.py", line 228, in inner
    return f(self, *args, **kwargs)
  File "C:\Users\wande\PycharmProjects\pythonProject\venv\lib\site-packages\pynput\_util\win32.py", line 390, in _handler
    self._handle(code, msg, lpdata)
  File "C:\Users\wande\PycharmProjects\pythonProject\venv\lib\site-packages\pynput\mouse\_win32.py", line 212, in _handle
    self.on_click(data.pt.x, data.pt.y, button, pressed)
  File "C:\Users\wande\PycharmProjects\pythonProject\venv\lib\site-packages\pynput\_util\__init__.py", line 144, in inner
    if f(*args) is False:
  File "C:\Users\wande\PycharmProjects\pythonProject\main.py", line 39, in on_click
    minhaPrint.save(os.path.join(diretorioRaiz, + "printkeylogger_" + horarioPrint + ".jpg"))
TypeError: bad operand type for unary +: 'str'

代码:

def on_click(x, y, buttom, pressed): if pressed: minhaPrint = py.screenshot() hora = datetime.now() horarioPrint = hora.strftime,("%H:%M:%S,") minhaPrint.save(os.path.join(diretorioRaiz, + "printkeylogger_" + horarioPrint + ".jpg")) 
pycharm
1个回答
1
投票

您收到的错误是 TypeError。这意味着 Python 正在尝试对不支持的数据类型执行操作。在这种情况下,发生错误是因为您尝试使用一元运算符连接字符串。

导致错误的代码行是:

minhaPrint.save(os.path.join(diretorioRaiz, + "printkeylogger" + horarioPrint + ".jpg"))

该行中的

+
符号是一元运算符。它用于否定一个值。然而,字符串不能被否定。因此,Python 会给你一个错误。

要修复此错误,您需要从代码行中删除

+
符号。这将允许 Python 正确地将字符串与文件路径连接起来。

更正后的代码行是:

minhaPrint.save(os.path.join(diretorioRaiz, "printkeylogger" + horarioPrint + ".jpg"))

完成此更改后,代码应该可以正常运行,不会出现任何错误。

© www.soinside.com 2019 - 2024. All rights reserved.