[英]Jump to function when key is pressed
我為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()
將master.bind(...)
命令添加到__init__
方法:
def __init__(self, master):
frame = Frame(master)
frame.pack()
master.bind('1', self.gpio25)
master.bind('2', self.gpio24)
master.bind('3', self.gpio23)
...
master.bind('1', self.gpio25)
將master.bind('1', self.gpio25)
1
事件綁定到方法調用self.gpio25(event)
。 您將需要7個master.bind
調用 - 每個鍵一個。
接下來,修改回調函數的定義。 您需要為每個參數添加第二個參數event
。 改變,例如,
def gpio25(self):
至
def gpio25(self, event = None):
說明:
使用bind
方法時,您將Event
綁定到回調函數(例如self.gpio25
)。 按鍵是KeyPress事件。 有關該事件的信息將發送到Event對象中的回調。 因此回調函數必須采用一個參數。 有關更多信息,請參閱Tkinter事件和綁定 。
Buttons
工作方式不同。 它們的回調函數使用零參數調用。
由於我們使用相同的回調函數self.gpio25
,作為self.gpio25
事件回調和Button回調,它必須是一個可以取零或一個參數的函數。
在Python中,這樣做的方法是使用調用簽名定義gpio25
,如:
def gpio25(self, event = None):
self
是App
類的一個實例。 self.gpio25
是一個綁定方法, self
綁定作為其第一個參數。 因此,調用self.gpio25()
將調用gpio25
函數,並將self
作為第一個參數(並且將為局部變量event
賦值None
)。 調用self.gpio25(foo)
將使用self
作為第一個參數調用gpio25
,並將foo
作為第二個參數調用(並且將為局部變量event
賦值foo
)。
在您的情況下,分配給event
的值並不重要,因為event
未在gpio25
的主體中gpio25
。 我們只需設置self.gpio25
就可以接受零個或一個參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.