self.ids 메소드를 사용하여 kivy의 위젯 내부에있는 화면 요소에 액세스하려면 어떻게해야합니까?

레브 바렌보임

나는 키비 스크린이 있습니다. 여기에는 일부 요소와 위젯이 포함되며,이 위젯에는 자체 요소가 포함됩니다. 내가 뭔가를 시도 할 때

    self.ids.element_directly_on_a_screen.text = 'something' 

작동하지만 시도 할 때

    self.ids.element_defined_inside_of_a_widget.text = 'something_else'

그것은 반환

     AttributeError: 'super' object has no attribute '__getattr__'

어떻게해야합니까?

편집 : 예

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import Screen

class SomeWidget(FloatLayout):
    pass

class SomeScreen(Screen):
    def ButtonOne(self):
        self.ids.label_one.text = 'Something'
    def ButtonTwo(self):
        self.ids.label_two.text = 'Something else'

class TestApp(App):
    pass

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

및 test.kv

<SomeWidget>:
    Label:
        text: 'This is label inside of a widget'
        id: label_two
        size_hint: (0.2, 0.2)
        pos_hint: {"x": 0.4, "y": 0.5}

SomeScreen:
    Label:
        text: 'This is just a label'
        id: label_one
        size_hint: (0.2, 0.2)
        pos_hint: {"x": 0.4, "y": 0.5}
    SomeWidget:
        size_hint: (0.2, 0.2)
        pos_hint: {"x": 0.4, "y": 0.2}
    Button:
        text: 'Click me to test the first Label!'
        size_hint: (0.35, 0.2)
        pos_hint: {"x": 0.2, "y": 0}
        on_release:
            root.ButtonOne()
    Button:
        text: 'Click me to test the second Label!'
        size_hint: (0.35, 0.2)
        pos_hint: {"x": 0.6, "y": 0}
        on_release:
            root.ButtonTwo()
Eyllanesc

ID는 루트를 통해 액세스 할 수 있습니다. 귀하의 경우 "label_two"는 SomeWidget을 통해서만 액세스 할 수 있으므로 먼저 해당 요소에 액세스해야하므로 SomeScreen에서 생성 된 SomeWidget에 "id"를 추가해야합니다.

def ButtonTwo(self):
    self.ids.some_widget.ids.label_two.text = 'Something else'
SomeScreen:
    # ...
    SomeWidget:
        id: some_widget
        size_hint: (0.2, 0.2)
        pos_hint: {"x": 0.4, "y": 0.2}
    # ...

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관