【问题标题】:KivyMD on changing screen not creating MDList Items widget using root.ids and showing errorKivyMD 在更改屏幕上未使用 root.ids 创建 MDList 项目小部件并显示错误
【发布时间】:2021-06-09 10:32:48
【问题描述】:

我正在尝试使用 Kivymd 在 Python 中构建一个应用程序,并且我刚刚开始学习这个 kivymd 框架,所以我面临的问题是当我尝试将屏幕从 LoginScreen 更改为 HomeScreen 时,它会引发错误。我无法理解我的代码有什么问题。

我希望它将屏幕更改为 HomeScreen,然后在按下“加入聊天”按钮时显示列表。

代码如下:

main.py

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen,ScreenManager
from kivymd.uix.list import MDList, OneLineIconListItem,IconLeftWidget


class LoginScreen(Screen):
    pass

class HomeScreen(Screen):
    pass



class DemoApp(MDApp):
    def build(self):
        self.theme_cls.theme_style="Dark"
        
        self.sm=ScreenManager()
        self.sm.add_widget(LoginScreen(name="login"))
        self.sm.add_widget(HomeScreen(name="home"))

        screen=Builder.load_file("helper_file.kv")
        return screen
    
    def do_login(self):
        self.sm.current = "home"
        for i in range(20):
            st_name="student "
            list_item = OneLineIconListItem(text=f"student {str(i)}")
            list_item.add_widget(IconLeftWidget(icon= "alpha-a-box"))
            self.root.ids.users_lst.add_widget(list_item)

DemoApp().run()

helper_file.kv

ScreenManager:
    LoginScreen:
    HomeScreen:

<LoginScreen>:
    name: "login"
    Screen:
        MDLabel:
            text: "Demo App"
            halign: "center"
            pos_hint: {"center_x": .5, "center_y": .8}
            theme_text_color: "Primary"
        MDTextField:
            hint_text: "Enter username"
            helper_text: "to join the chat (test mode)"
            helper_text_mode: "on_focus"
            icon_right: "android"
            icon_right_color: app.theme_cls.primary_color
            pos_hint:{'center_x': 0.5, 'center_y': 0.5}
            size_hint_x:None
            width:311
        MDRoundFlatButton:
            text: "Join Chat"
            pos_hint: {"center_x": .5, "center_y": .4}
            on_release: app.do_login()
        MDLabel:
            text: "This is testing mode only"
            halign: "center"
            pos_hint: {"center_x": .5, "center_y": .2}
            theme_text_color:"Hint"

<HomeScreen>:
    name: "home"
    Screen:
        BoxLayout:
            orientation: "vertical"
            MDToolbar:
                title: "Demo Chat Application"
            ScrollView:
                MDList:
                    id: users_lst

运行此程序时,登录屏幕运行良好,但按下“加入聊天”按钮时会引发以下错误:

self.root.ids.users_lst.add_widget(list_item)
   File "kivy\properties.pyx", line 864, in kivy.properties.ObservableDict.__getattr__
 AttributeError: 'super' object has no attribute '__getattr__'

【问题讨论】:

  • 请注意,在您的 build() 方法中,您构建了两次 GUI。一次是Buider.load_file(),一次是涉及self.sm 的行。应该删除涉及self.sm 的行。您的login() 方法中的self.sm.current = "home" 行也应该被删除(它没有做任何事情)。
  • 或者你可以用self.root.current = "home"替换self.sm.current = "home"

标签: python-3.x kivy kivy-language kivymd


【解决方案1】:

| 1 |

class HomeScreen(Screen):
    pass
<HomeScreen>:
    name: "home"
    Screen:
        BoxLayout:
            orientation: "vertical"
            MDToolbar:
                title: "Demo Chat Application"
            ScrollView:
                MDList:
                    id: users_lst

你有一个屏幕有一个屏幕,那不是你想要做的。

<HomeScreen>:
    name: "home"
    BoxLayout:
        orientation: "vertical"
        MDToolbar:
            title: "Demo Chat Application"
        ScrollView:
            MDList:
                id: users_lst

############################################## ######## ################################################# #####

| 2 |

父母无权访问内在儿童儿童的id。 (不清楚我知道,所以我会给你很多打印以帮助你理解)。

    def do_login(self):
        self.root.current = "home"
        for i in range(20):
            st_name = "student "
            list_item = OneLineIconListItem(text=f"student {str(i)}")
            list_item.add_widget(IconLeftWidget(icon="alpha-a-box"))

            print("this is my App:", self)
            print("this is my ScreenManager:", self.sm)
            print("this is my global app visual widget (which is equal to ScreenManager here):", self.root)
            print("this is the ScreenManager's dictionnary containing the widgets referenced with their id:", self.root.ids)
            print("this is the current Screen:",self.root.current_screen)
            print("this is current Screen dictionnary containing the widgets referenced with their id:", self.root.current_screen.ids)
            print("this is the actual MDList", self.root.current_screen.ids["users_lst"])
            self.root.current_screen.ids["users_lst"].add_widget(list_item)

【讨论】:

    猜你喜欢
    • 2021-05-09
    • 2021-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-29
    相关资源
    最近更新 更多