Kivy:AttributeError:'CreateButton'对象没有属性'_disabled_count'

问题描述 投票:0回答:1

我正在尝试在Raspberry Pi触摸屏上创建一个按钮仪表板作为家庭爱好,我想创建一些您可以按下并释放的按钮,一些可以打开和关闭的按钮,还有一些具有多种状态的按钮。

我想在按下按钮时更改其状态。因此,我可以通过CreateButton类检测到按钮被按下,但是我想在init处传递按钮状态信息。当我这样做时,我得到

AttributeError:'CreateButton'对象没有属性'_disabled_count'

如果删除CreateButton中的init,则代码运行,我不理解。你能建议我要去哪里吗?

非常感谢

Main.py

import pickle
from kivy.app import App
from kivy.uix.image import AsyncImage
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from typing import List

# Define the data structure of a button state
class StateDefinition(object):
    def __init__(self, Label, Image, Colour, Action):
        self.Label = Label
        self.Image = Image
        self.Colour = Colour
        self.Action = Action

# Define the data structure of a button
class ButtonDefinition(object):
    def __init__(self, buttonname: str, currentstate: int, buttonstates: List[StateDefinition]):
        self.currentstate = currentstate
        self.buttonname = buttonname
        self.buttonstates = buttonstates

# Define the data structure of a dashboard - a collection of related buttons
class DashboardDefinition(object):
    def __init__(self, dashboardname: str, dashboardbuttons: List[ButtonDefinition]):
        self.dashboardname = dashboardname
        self.dashboardbuttons = dashboardbuttons

# This class creates the kivy button and binds it to the action
class CreateButton(Button):
    def __init__(self, **kwargs):
        print("Got Here")

    def on_touch_down(self, touch):
        if self.collide_point(*touch.pos):
            if touch.button == "right":
                print(self.id, "right mouse clicked")
            elif touch.button == "left":
                print(self.id, "left mouse clicked")
            else:
                print(self.id)
            return True
        return super(CreateButton, self).on_touch_down(touch)

## This is the class that builds the Carousel of buttons
class BuildLayout(GridLayout):

    def __init__(self, **kwargs):
        super(BuildLayout, self).__init__(**kwargs)
        self.build_dashboard()

    def build_dashboard(self):

        # Define all the test data
        state1 = StateDefinition(Label = 'State 1', Image = 'Image1.png', Colour = '#FF0000', Action = 'A')
        state2 = StateDefinition(Label = 'State 2', Image = 'Image2.png', Colour = '#00FF00', Action = 'B')
        state3 = StateDefinition(Label = 'State 3', Image = 'Image3.png', Colour = '#0000FF', Action = 'C')
        button1 = ButtonDefinition(buttonname = "Button 1", currentstate = 0, buttonstates = [state1])
        button2 = ButtonDefinition(buttonname = 'Button 2', currentstate = 0, buttonstates = [state2, state1])
        button3 = ButtonDefinition(buttonname = 'Button 3', currentstate = 0, buttonstates = [state3, state2, state1])
        dashboard1 = DashboardDefinition(dashboardname = "Test Dashboard", dashboardbuttons = [button1, button2, button3])

        for buttonID in range(0, len(dashboard1.dashboardbuttons)):
            buttonwidget = CreateButton(id = str(buttonID))
            buttonwidget.text = dashboard1.dashboardbuttons[buttonID].buttonname + "\nButton State: " + \
                                str(dashboard1.dashboardbuttons[buttonID].currentstate)
            self.add_widget(buttonwidget)

# This is tha main class that sets up the app
class LayoutApp(App):
    def build(self):
        return BuildLayout()

if __name__ == '__main__':
    LayoutApp().run()

Layout.kv

#:kivy 1.11.0

<CreateButton>:
    font_size: 18
    on_touch_down: self.on_touch_down

<BuildLayout>:
    rows: 3
    cols: 3
    row_force_default: True
    row_default_height: 150
    col_force_default: True
    col_default_width: 150
    padding: [10]
    spacing: [10]
python button kivy
1个回答
0
投票

当我尝试使用add_widget创建Button时遇到相同的问题

我的错误提示

© www.soinside.com 2019 - 2024. All rights reserved.