我为我想要测试的 Raspberry Pi 的 GPIO 制作了一些硬件。我发现了一些简洁的 python 代码,它为 8 个输出创建了 8 个按钮,并让您切换它们的状态。我对 python 几乎一无所知,但我希望能够通过键盘切换 8 个输出(例如数字 1-8)。我不知道如何在不暂停程序流程的情况下要求键盘输入,然后在响应后继续。
如何让数字 1-8 “中断”我的程序并跳转到 8 个相应函数中的 1 个?
我的代码:
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()