我正在尝试创建一个 GUI,在查看了这里的各种帖子后,我仍然感到困惑。self.state我的问题是,当我设置为不同的值时,我为反映 GPIO 按钮状态而制作的自定义按钮不会更新其外观。我认为这可能与对象构造有关,但我不知道如何修复它。
main.py
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ObjectProperty
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.clock import Clock
from kivy.core.window import Window
import RPi.GPIO as GPIO
# make app run in fullscreen mode
Window.fullscreen = 'auto' # uses display's current resolution
# Set up GPIO
ok_btn_pin = 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(ok_btn_pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(ok_btn_pin, GPIO.BOTH) #detect if GPIO.RISING or GPIO.FALLING occur
class GPIOButton(Button):
btn_gpio_pin = NumericProperty(-1)
def __init__(self, **kwargs):
super(GPIOButton, self).__init__(**kwargs)
print("GPIOButton __init__ called")
print("btn_gpio_pin =", self.btn_gpio_pin)
def update(self, dt):
#print("GPIOButton update() called")
if GPIO.input(self.btn_gpio_pin) == GPIO.HIGH and GPIO.event_detected(self.btn_gpio_pin):
self.state = 'down'
print("Pin", self.btn_gpio_pin, self.state)
elif GPIO.input(self.btn_gpio_pin) == GPIO.LOW and GPIO.event_detected(self.btn_gpio_pin):
self.state = 'normal'
print("Pin", self.btn_gpio_pin, self.state)
class LeftSidebar(FloatLayout):
ok_btn = GPIOButton(btn_gpio_pin = ok_btn_pin)
def __init__(self, **kwargs):
super(LeftSidebar, self).__init__(**kwargs)
print("LeftSidebar __init__ called")
def update(self, dt):
#print("LeftSidebar update() called")
self.ok_btn.update(dt)
海绵宝宝撒
相关分类