英文:
Getting 'Nonetype is not callable' error but I don't know why
问题
我正在尝试创建一个反应时间游戏,但是在添加热键后,当我按下回车键时,出现了一个超长的NoneType错误,涉及到多个不同的文件。
```python
import time
import datetime
import random
import keyboard
playing = True
def stop(playing):
playing = False
input("按下回车键开始测试您的反应时间!")
print("当您看到猫时,请按回车键!")
time.sleep(1)
keyboard.add_hotkey('enter', stop(playing))
time.sleep(random.randint(3, 6))
if playing == True:
keyboard.remove_hotkey('enter')
start = datetime.datetime.now()
input("按回车键!!!!")
end = datetime.datetime.now()
print(end - start)
热键的目的是阻止用户仅仅按住回车键不放。我不知道为什么这不起作用。
我尝试在函数的末尾添加'return playing',但这只会给我一个类似'bool is not callable'的错误。
<details>
<summary>英文:</summary>
I'm trying to create a reaction time game thing, however when I press enter after the hotkey is added I get a super long Nonetype error making reference to several different files.
import time
import datetime
import random
import keyboard
playing=True
def stop(playing):
playing=False
input("Press enter when you're ready to test your reaction time!")
print("Press enter when you see the cat!")
time.sleep(1)
keyboard.add_hotkey('enter', stop(playing))
time.sleep(random.randint(3,6))
if(playing==True):
keyboard.remove_hotkey('enter')
start = datetime.datetime.now()
input("Press Enter!!!!")
end = datetime.datetime.now()
print(end-start)
The idea of the hotkey is to stop the user from being able to just hold the enter button down. I don't know why this doesn't work.
I tried adding 'return playing' to the end of the function but that just gave me a similar error of 'bool is not callable'
</details>
# 答案1
**得分**: 0
The code you provided can be translated as follows:
```plaintext
`stop(playing)` 返回 `None`,所以当你这样做时:
```python
keyboard.add_hotkey('enter', stop(playing))
就等同于你这样做:
stop(playing)
keyboard.add_hotkey('enter', None)
add_hotkey
的第二个参数应该是一个函数(而不是调用一个函数的结果),所以如果你传递 None
,它会引发错误。
我认为你想要做的是定义你的 stop
函数,使它可以不带参数调用,并将全局变量 playing
设置为 False:
def stop():
global playing
playing = False
然后将该函数(不要立即调用它)传递给 add_hotkey
:
keyboard.add_hotkey('enter', stop)
<details>
<summary>英文:</summary>
`stop(playing)` returns `None`, so when you do:
keyboard.add_hotkey('enter', stop(playing))
it's exactly as if you did:
stop(playing)
keyboard.add_hotkey('enter', None)
The second argument to `add_hotkey` is supposed to be a function (not the result of calling a function), so if you pass it `None`, it raises an error.
What I think you want to do is define your `stop` function so that it can be called with no arguments, and sets the global `playing` variable to False:
def stop()
global playing
playing = False
and then pass the function (without calling it yet) to `add_hotkey`:
keyboard.add_hotkey('enter', stop)
</details>
# 答案2
**得分**: 0
This prematurely **calls** the `stop` function and then passes `None` as the callback argument to `add_hotkey`. Then when the event is triggered `None` it is being attempted to be called.
Instead, you should use
```python
keyboard.add_hotkey('enter', stop, args=(playing,))
as shown on the documentation.
英文:
keyboard.add_hotkey('enter', stop(playing))
This prematurely calls the stop
function and then passes None
as the callback argument to add_hotkey
. Then when the event is triggered None
it is being attempted to be called.
Instead, you should use
keyboard.add_hotkey('enter', stop, args=(playing,))
as shown on the documentation.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论