Yan Wolf
Yan Wolf

Reputation: 103

How do I access and add a widget to MDGridLayout by ID : BOX in KivyMD

How do I access and add a widget to MDGridLayout by ID : BOX in my case???

I tried to achieve the desired result in various ways, but it constantly gives an error, root does not see ID: BOX self.root.ids. ......

print(self.test2.ids) raises AttributeError: 'TestApp' object has no attribute 'test2'

While print(self.root.ids) returns

{'screen_manager': <WeakProxy to <kivy.uix.screenmanager.ScreenManager object at 0x00000221CA016580>>,
'toolbar': <WeakProxy to <kivymd.uix.toolbar.MDToolbar object at 0x00000221CA0B6E40>>,
'nav_drawer': <WeakProxy to <kivymd.uix.navigationdrawer.MDNavigationDrawer object at 0x00000221CA133DD0>>}

test.py

from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout


Builder.load_file('test2.kv')



class ContentNavigationDrawer(MDBoxLayout):
    screen_manager = ObjectProperty()
    nav_drawer = ObjectProperty()


class TestApp(MDApp):

    def build(self):
        return

TestApp().run()

test.kv

<ContentNavigationDrawer>
    md_bg_color: 0.231, 0.231, 0.231, 1

    ScrollView:

        MDList:

            OneLineAvatarIconListItem:
                theme_text_color: "Custom"
                text_color: 1, 1, 1, 1
                text: "Main Menu"

                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "scr1"

                IconLeftWidget:
                    theme_text_color: "Custom"
                    text_color: 1, 1, 1, 1
                    icon: "menu-open"

            OneLineAvatarIconListItem:
                theme_text_color: "Custom"
                text_color: 1, 1, 1, 1
                text: "Test"

                on_press:
                    root.nav_drawer.set_state("close")
                    root.screen_manager.current = "test2"

                IconLeftWidget:
                    theme_text_color: "Custom"
                    text_color: 1, 1, 1, 1
                    icon: "fire"


MDScreen:
    md_bg_color: 0.164, 0.164, 0.164, 1

    MDNavigationLayout:
        x: toolbar.width

        ScreenManager:

            id: screen_manager

            MDScreen:
                name: "scr1"

                MDBoxLayout:
                    cols: 1
                    orientation: 'vertical'
                    md_bg_color: 0.235, 0.247, 0.254, 1



            Test2:


    MDToolbar:
        id: toolbar
        title: "SCUM INFO"
        pos_hint: {"top": 1}
        md_bg_color: 0.231, 0.231, 0.231, 1
        specific_text_color: 1, 1, 1, 1
        left_action_items: [["menu", lambda x: nav_drawer.set_state("open")]]
        right_action_items:
            [["magnify", lambda x: banner.show()], ["cog-outline", lambda x: app.set_screen("setting")]]


    MDNavigationDrawer:
        id: nav_drawer
        ContentNavigationDrawer:
            screen_manager: screen_manager
            nav_drawer: nav_drawer

test2.kv (second screen)

<Test2@MDScreen>:

    name: "test2"

    MDBoxLayout:
        cols: 1
        orientation: 'vertical'

        MDBoxLayout:
            cols: 1
            orientation: 'vertical'
            size_hint: 1, .11

        MDBoxLayout:
            cols: 1
            orientation: 'vertical'
            size_hint: 1, .89

            ScrollView:

                MDGridLayout:
                    id:box   # <---------------- ID ID ID ID
                    cols: 1
                    size_hint_y: None
                    height: '200dp'
                    padding: 5, 5, 5, 5
                    spacing: dp(15)
        # VVVVVVVVVVVVVVVVVVVVVVVVVVVV DUPLICATE,DUPLICATE ---->
                    MDGridLayout:
                        cols: 1
                        padding: 5, 5, 5, 5
                        md_bg_color: 0.231, 0.231, 0.231, 1

                        spacing: dp(5)
                        canvas.before:
                            Color:
                                rgba: .5, .5, .5, 1
                            Line:
                                width: 1
                                rectangle: self.x, self.y, self.width, self.height

                        MDBoxLayout:
                            cols: 1
                            orientation: 'vertical'
                            size_hint: 1, .30
                            md_bg_color: 0.235, 0.247, 0.254, 1

                            MDRelativeLayout:
                                cols: 1
                                orientation: 'vertical'

    # vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

                                FitImage:
                                    source: 'img.jpg'    # <----------

                                MDLabel:
                                    id: news_title
                                    text: "Some text"    #  <-------

    # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                                    theme_text_color: "Custom"
                                    text_color: app.theme_cls.accent_color
                                    pos_hint: {"center_y": .5}
                                    font_style: "H6"
                                    font_size: root.width/18

Upvotes: 0

Views: 581

Answers (1)

ApuCoder
ApuCoder

Reputation: 2888

It seems you want to access the ids of the dynamic class Test2. You may achieve this as follows:

First refer it by creating its id in kvlang as,

            Test2:
                id: test2

Now you should be able to access its inner ids as usual by self.root.ids.test2.ids (when done in App's subclass i.e. here in TestApp)

Upvotes: 2

Related Questions