“Getting ‘Nonetype is not callable’ error but I don’t know why.”

huangapple go评论70阅读模式
英文:

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&#39;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&#39;t know why this doesn&#39;t work. 

I tried adding &#39;return playing&#39; to the end of the function but that just gave me a similar error of &#39;bool is not callable&#39;

</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&#39;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(&#39;enter&#39;, 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(&#39;enter&#39;, stop, args=(playing,))

as shown on the documentation.

huangapple
  • 本文由 发表于 2023年5月18日 13:08:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76277896.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定