如何在类或函数中使用 gpiozero.Button.when_pressed

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

我正在开发一个气象站(Raspberry pi),并且正在使用Python。我开发了一个正在运行的程序,但现在我决定更新我的代码并构建所有内容(创建类等)。 但现在我遇到了两个关于 gpiozero Button 的问题。
首先,当我尝试将函数分配给函数或类中的

Button.when_pressed
时,按下按钮时不会调用该函数。

以下代码只是一个示例,为了使代码尽可能少:

from gpiozero import Button import time number = 0 def function_that_gets_called(): global number number += 1 def main(): wind_sensor = Button(6) wind_sensor.when_pressed = function_that_gets_called main() while True: # Sleep, so the program doesn't exit immediately time.sleep(60) # Do some other stuff with number
为什么按下按钮时 

function_that_gets_called

 不会被调用。同样,当我尝试在课堂上分配 
when_pressed
 时,它也不起作用。

其次,为什么我必须使用

global number

?否则数字变量不会改变,但是有其他解决方案可以更好地做到这一点吗?

非常感谢!

编辑:编辑 time.sleep(60)。我明确指出,我所说的“不起作用”是什么意思。这是我在 stackoverflow 上的第一个问题,所以如果不准确,请原谅,如果能告诉我如何改进我的问题,那就太好了。

我不确定当你调用
python button gpiozero
1个回答
1
投票
后主函数退出时,wind_sensor 会发生什么。我的猜测是它会被摧毁。无论如何,如果您删除 main 及其调用,您的代码就可以工作。尝试在递增后添加一个

print(number)

,以便按下按钮时可以看到一些内容
关于这个课程...

我在课堂上被压时也遇到了这个问题。但我已经让它在我的案例中发挥作用了。 我的应用程序可以具有可变数量的按钮,并且它们都通过在代码开头实例化的对象进行管理。应用程序不会退出,因为有一个带有 ws.runForever() 的底层 websocket 客户端。在你的情况下,你可以使用像这样的循环

while 1: pass

这将使用大约 12% 到 15% 的 CPU 或

pause()

 来保持代码运行,而不使用处理器(使用 ctrl+c 退出)
现在我使用一个字典来处理类中的可变数量的按钮以及我的处理程序。
像这样的东西可以使用类,继续运行,并且您可以扩展代码以拥有动态数量的按钮。

from gpiozero import Button import json class myController(): theButtons={} yourNumber=0 def __init__(self): self.yourNumber=0 #here is the right place to initialize the variables! def handler(self,Button): # here the first parameter is the class itself and then the button that generated the interrupt --> this works like that, but could not confirm that self needs to exist before the mandatory arg "Button" because it is inside the class, or by other reason. Can somebody confirm? not clear in documentation. print("Pressed button in pin: ",str(Button.pin.number)) self.yourNumber +=1 def addButton(self,msg): # Example json: '[{"id":"button1","pin":2},{"id":"button2","pin":4}]' the msg is the message that is received via websocket with the configuration of buttons. you can make it at you own needs. id and pin are string that I choose for machines dialog btns=json.loads(msg) for b in btns: self.theButtons[b.get('id')]=Button(b.get('pin')) self.theButtons[b.get('id')].when_pressed=self.handler #and that's it for the class no let's instantiate and make the code persist running if __name__ == "__main__": # this is executed only if you call this file. if you use it as import it will not run. it's a best practice, should you need to import the class in other file and don't want the code to execute. btnController=myController() pause()

类在这个用例上有点矫枉过正,但我相信它可以在不使用全局变量的情况下解决您的问题。
    

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