【问题标题】:Kivy: AttributeError: 'CreateButton' object has no attribute '_disabled_count'Kivy:AttributeError:“CreateButton”对象没有属性“_disabled_count”
【发布时间】:2020-04-18 17:18:31
【问题描述】:

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

我想改变按钮按下时的状态。所以我可以通过 CreateButton 类检测到按钮被按下,但我想在 init 处传递按钮状态信息。当我这样做时,我得到 ​​p>

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】:

    当您为 Kivy 元素覆盖 __init__ 方法时,它不会执行继承的 __init__ 方法,除非您手动告诉它这样做。如果您不这样做,它将无法正确设置 Kivy 期望的所有属性(例如 _disabled_count)。

    我希望如果您像在BuildLayout 中一样,将super().__init__(**kwargs)(或super(&lt;classname&gt;, self).__init__(**kwargs) 用于Python 2 兼容性)作为新__init__ 方法的第一行添加,这将起作用。很容易忘记!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 2016-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多