Kivy AttributeError: объект 'NoneType' не имеет атрибута 'ids' - PullRequest
0 голосов
/ 27 мая 2020

Я создал пользовательскую кнопку в файле kv, я хочу установить disabled = True, если приложение. root .ids.my_label == "disabled" else disabled = False. Однако я продолжаю получать AttributeError: объект «NoneType» не имеет идентификаторов атрибутов. Я не могу это сделать, я не просто делаю это правильно, буду благодарен за любую помощь. Спасибо!

test.py

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import ObjectProperty

Builder.load_string('''
<CustomBtn@Button>
       disabled: True if app.root.ids.word.my_label == "disable" else False

<main>:
my_label:my_label
BoxLayout:
    orientation:"vertical"

    Label:
        id: my_label
        text: "Disabled"
    CustomBtn:
        text: "Btn1"
    CustomBtn:
        text: "Btn2"
    CustomBtn:
        text: "Btn3"
    Button:
        text: "Disable/Enable"
        on_press: root.disablebtn()
        ''')


class main(BoxLayout):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        my_label = ObjectProperty()

    def disablebtn(self):
        print(self.my_label)
        if self.my_label.text == "Disabled":
            self.my_label.text = "Enabled"
        else:
            self.my_label.text = "Disabled"


class CallApp(App):
    def build(self):
        return main()


CallApp().run()

Ответы [ 2 ]

0 голосов
/ 28 мая 2020

Ваш исходный подход не будет работать, потому что app.root не установлен, когда применяется ваше правило <CustomBtn@Button>. Вот модифицированная версия вашего кода, которая делает то, что вы хотите, без этой проблемы:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.button import Button


class CustomBtn(Button):
    pass

Builder.load_string('''
<main>:
    my_label:my_label
    BoxLayout:
        orientation:"vertical"

        Label:
            id: my_label
            text: "Disabled"
        CustomBtn:
            text: "Btn1"
        CustomBtn:
            text: "Btn2"
        CustomBtn:
            text: "Btn3"
        Button:
            text: "Disable/Enable"
            on_press: root.disablebtn()
        ''')


class main(BoxLayout):
    my_label = ObjectProperty()

    def disablebtn(self):
        if self.my_label.text == "Disabled":
            self.my_label.text = "Enabled"
            self.disable_customBtns(False)
        else:
            self.my_label.text = "Disabled"
            self.disable_customBtns(True)

    def disable_customBtns(self, is_disabled):
        # Look for CustomBtn instances, and set their `disabled` value
        for child in self.walk():
            if isinstance(child, CustomBtn):
                child.disabled = is_disabled

    def on_my_label(self, *args):
        # this just sets the initial value of disabled to True
        self.disable_customBtns(True)



class CallApp(App):
    def build(self):
        return main()

CallApp().run()
0 голосов
/ 28 мая 2020

Вы можете сделать это, поместив выражение под каждым CustomBtn, например:

    CustomBtn:
        text: "Btn1"
        disabled: True if root.my_label.text == "Disabled" else False

, а не в правиле <CustomBtn>:.

...