如何通过 KivyMD 中的 ID : BOX 访问小部件并将其添加到 MDGridLayout

发布于 2025-01-11 05:21:41 字数 5373 浏览 1 评论 0原文

在我的例子中,如何通过 ID : BOX 访问小部件并将其添加到 MDGridLayout ???

我尝试通过各种方式达到预期的结果,但它不断给出错误,root看不到ID:BOX self.root.ids。 ......

print(self.test2.ids) 引发 AttributeError: 'TestApp' 对象没有属性 'test2'

print(self.root .ids) 返回

{'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(第二个屏幕)

<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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

囚你心 2025-01-18 05:21:41

您似乎想要访问动态类 Test2ids。您可以按如下方式实现此目的:

首先通过在 kvlang 中创建其 id 来引用它,

            Test2:
                id: test2

现在您应该能够像往常一样通过 self 访问其内部 id。 root.ids.test2.ids (在 App 的子类中完成时,即在 TestApp 中)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文