samirhossein__
samirhossein__

Reputation: 76

how access an element with id in kivy & kivymd

I am using KIVY Lib I want access some element in test class. How I should do it?

this is my code

from kivymd.app import MDApp
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.floatlayout import FloatLayout
from kivy.lang.builder import Builder

test_helper ="""
MDLabel:
    id : label
    text: "Hello World !"

"""

class test(FloatLayout):
    def __init__(self, **kwargs):
        super(test, self).__init__(**kwargs)
        self.add_widget(Builder.load_string(test_helper))

class MainApp(MDApp):
    def build(self):
        self.sm = ScreenManager()
        self.test = test()
        screen = Screen(name = "test")
        screen.add_widget(self.test)
        self.sm.add_widget(screen)
        return self.sm
    
if __name__ == "__main__":
    app = MainApp()
    app.run()

for example I have MDlabel in test_helper who its for test class. but i do not know how access it

Upvotes: 1

Views: 252

Answers (1)

inclement
inclement

Reputation: 29488

 self.test = test()

With this declaration, you could access the MDLabel with self.test.children[0].

Upvotes: 1

Related Questions