亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Kivy:自定義按鈕不隨 self.state 更改而更新

Kivy:自定義按鈕不隨 self.state 更改而更新

倚天杖 2023-09-26 16:26:53
我正在嘗試創建一個 GUI,在查看了這里的各種帖子后,我仍然感到困惑。self.state我的問題是,當我設置為不同的值時,我為反映 GPIO 按鈕狀態而制作的自定義按鈕不會更新其外觀。我認為這可能與對象構造有關,但我不知道如何修復它。main.pyimport kivykivy.require('1.11.1')from kivy.app import Appfrom kivy.uix.widget import Widgetfrom kivy.properties import NumericProperty, ObjectPropertyfrom kivy.uix.floatlayout import FloatLayoutfrom kivy.uix.button import Buttonfrom kivy.clock import Clockfrom kivy.core.window import Windowimport RPi.GPIO as GPIO# make app run in fullscreen modeWindow.fullscreen = 'auto'  # uses display's current resolution# Set up GPIOok_btn_pin = 4GPIO.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 occurclass 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)
查看完整描述

1 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

of被調用兩次__init__()。當您的方法被調用并執行時GPIOButton一次。這將創建通過規則出現在 GUI 中的。該方法在您的類中執行時會再次調用。第二次調用創建的實例不會出現在 GUI 中,但它是該方法中引用的實例。build()self.root = LifterGUI()GPIOButtonkv__init__()ok_btn = GPIOButton(btn_gpio_pin = ok_btn_pin)LeftSidebarGPIOButtonupdate()


GPIOButton由于您已經在 中設置了對 的引用kv,因此您可以修改該類LeftSidebar以使用該引用:


class LeftSidebar(FloatLayout):


    ok_btn_button = ObjectProperty(None)


    def __init__(self, **kwargs):

        super(LeftSidebar, self).__init__(**kwargs)

        print("LeftSidebar __init__ called")


    def update(self, dt):

        #print("LeftSidebar update() called")

        self.ok_btn_button.update(dt)

ok_btn_button在 your和kvtheok_btn_button中設置對 中內置的 的LeftSidebar引用。這樣您就可以參考類中使用的按鈕。GPIOButtonkvself.ok_btn_buttonLeftSidebar


請注意,您的LifterGUI.


查看完整回答
反對 回復 2023-09-26
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號