我为我的树莓派GPIO制作了一些硬件,我想测试一下。我找到了一些不错的Python代码,可以为8个输出创建8个按钮,并允许您切换它们的状态。我几乎没有Python知识,但我想通过键盘(例如数字1-8)来切换这8个输出。我不知道如何请求键盘输入而不暂停程序流,然后在响应后继续。
如何使数字1-8“中断”我的程序并跳转到8个对应函数之一?
我的代码:
from Tkinter import *
import RPi.GPIO as GPIO
import time
GPIO.setmode( GPIO.BCM )
GPIO.setup( 4, GPIO.OUT)
GPIO.setup(17, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(21, GPIO.OUT)
GPIO.setup(22, GPIO.OUT)
GPIO.setup(23, GPIO.OUT)
GPIO.setup(24, GPIO.OUT)
GPIO.setup(25, GPIO.OUT)
class App:
io4=0
io17=0
io18=0
io21=0
io22=0
io23=0
io24=0
io25=0
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.p1 = Button(frame, text="GPIO 25",fg="green", command=self.gpio25)
self.p1.pack(side=LEFT)
self.p1.grid(row=0,column=0)
self.p2 = Button(frame, text="GPIO 24",fg="red", command=self.gpio24)
self.p2.pack(side=LEFT)
self.p2.grid(row=0,column=1)
self.p3 = Button(frame, text="GPIO 23",fg="red", command=self.gpio23)
self.p3.pack(side=LEFT)
self.p3.grid(row=0,column=2)
self.p4 = Button(frame, text="GPIO 22",fg="red", command=self.gpio22)
self.p4.pack(side=LEFT)
self.p4.grid(row=0,column=3)
self.p5 = Button(frame, text="GPIO 21",fg="red", command=self.gpio21)
self.p5.pack(side=LEFT)
self.p5.grid(row=0,column=4)
self.p6 = Button(frame, text="GPIO 18",fg="red", command=self.gpio18)
self.p6.pack(side=LEFT)
self.p6.grid(row=0,column=5)
self.p7 = Button(frame, text="GPIO 17",fg="red", command=self.gpio17)
self.p7.pack(side=LEFT)
self.p7.grid(row=0,column=6)
self.p8 = Button(frame, text="GPIO 4", fg="red",command=self.gpio4)
self.p8.pack(side=LEFT)
self.p8.grid(row=0,column=7)
def gpio4(self):
if self.io4==0:
GPIO.output(4, GPIO.HIGH)
self.io4=1
else:
GPIO.output(4, GPIO.LOW)
self.io4=0
return
def gpio17(self):
if self.io17==0:
GPIO.output(17, GPIO.HIGH)
self.io17=1
else:
GPIO.output(17, GPIO.LOW)
self.io17=0
return
def gpio18(self):
if self.io18==0:
GPIO.output(18, GPIO.HIGH)
self.io18=1
else:
GPIO.output(18, GPIO.LOW)
self.io18=0
return
def gpio21(self):
if self.io21==0:
GPIO.output(21, GPIO.HIGH)
self.io21=1
else:
GPIO.output(21, GPIO.LOW)
self.io21=0
return
def gpio22(self):
if self.io22==0:
GPIO.output(22, GPIO.HIGH)
self.io22=1
else:
GPIO.output(22, GPIO.LOW)
self.io22=0
return
def gpio23(self):
if self.io23==0:
GPIO.output(23, GPIO.HIGH)
self.io23=1
else:
GPIO.output(23, GPIO.LOW)
self.io23=0
return
def gpio24(self):
if self.io24==0:
GPIO.output(24, GPIO.HIGH)
self.io24=1
else:
GPIO.output(24, GPIO.LOW)
self.io24=0
return
def gpio25(self):
if self.io25==0:
GPIO.output(25, GPIO.HIGH)
self.io25=1
else:
GPIO.output(25, GPIO.LOW)
self.io25=0
return
def reserved(self):
return
root = Tk()
app = App(root)
root.mainloop()