【发布时间】:2017-08-01 14:45:35
【问题描述】:
我正在尝试在 Raspberry Pi 上编写一个按钮以将一个整数添加到另一个整数,以便我可以通过检查变量 mod 2 是否为 0 在while 循环中的条件之间来回翻转。我实际上是在尝试通过检查变量是奇数还是偶数来翻转while 循环中的条件。
我正在尝试使用gpiozero 库的when_pressed 函数,但它似乎无法调用添加和输出整数的函数。
所以,我的代码是:
from gpiozero import Button
btn = Button(17) #the button is wired to GPIO pin 17
def addSurf(a):
a = a + 1
return(a)
x = 0
btn.when_pressed = addSurf(x)
while True:
if x == 0:
#do some stuff
else:
#do some other stuff
为什么我尝试运行它,我得到TypeError: unsupported operand type(s) for +: 'Button' and 'int'。
如何使用btn.when_pressed 函数来使用输入和输出整数的函数?
或者,是否有其他一些 [更好的?] 方法可以使按钮在 while 循环中切换两种状态?
【问题讨论】:
标签: python function while-loop raspberry-pi gpio