1 回答
TA贡献1863条经验 获得超2个赞
在此解决方案中,您只需打开和关闭输出
from gpiozero import Button
from signal import pause
import time
pin = #Set a pin
r = LED(pin)
b1 = Button(23)
b1.when_pressed = r.on
b2 = Button(24)
b2.when_pressed = r.off
pause()
这里启动一个线程来做一些事情:
from gpiozero import Button
from signal import pause
import time
import _thread
run = False
def do_stuff():
while run: #do stuff...
time.sleep(1)
print("waiting...")
def first_button():
global run
print("First Pressed")
run = True
_thread.start_new_thread(do_stuff)
def second_button():
global run
print("Second Pressed")
run = False
b1 = Button(23)
b1.when_pressed = first_button
b2 = Button(24)
b2.when_pressed = second_button
pause()
添加回答
举报