引用错误:使用OneLineListItem时,KivyMd中不再存在弱引用对象错误

2024-04-25 13:05:47 发布

您现在位置:Python中文网/ 问答频道 /正文

当我运行使用kivymd制作的应用程序时,当我使用OneLineListItem时,它会显示错误,当我删除它时,因此应用程序运行平稳。我使用的是Python版本3.8。当我将OneLineListItem放在ScrollView之后,MDList在MyList块中,因此它给出了错误。当我编写另一个代码时,只有MDList和onelinelistitem,所以它正在运行,请解决此问题。 这是log说的-

ReferenceError:弱引用对象不再存在。 这是守则-

from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.list import OneLineListItem
from kivymd.uix.navigationdrawer import NavigationLayout
KV='''
ScreenManager:
    Home:
    Submit:
<Home>:
    name:'home'
    NavigationLayout:
        ScreenManager:
            MDScreen:
                AnchorLayout:
                    anchor_x:'center'
                    anchor_y:'top'
                    MDToolbar:
                        id:toolbar
                        title:'My Register'
                        md_bg_color:app.theme_cls.primary_dark
                        height:'75dp'
                        left_action_items:[['menu',lambda x:navi_draw.set_state()]]
                        elevation:10
                    MDScreen:
                        MDTextField:
                            id:student_name
                            hint_text:'Enter student name'
                            helper_text:'Should only contain alphabetical literals'
                            helper_text_mode:'on_focus'
                            pos_hint:{'center_x':0.5,'center_y':0.5}
                            size_hint_x:None
                            width:460
                            icon_right:'face'
                            icon_right_color:app.theme_cls.primary_color
                        MDRaisedButton:
                            text:'Submit'
                            pos_hint:{'center_x':0.5,'center_y':0.4}
                            
                            on_press:
                                
                                root.manager.current='submit'
    
                                root.manager.transition.direction = 'left'
                            
        MDNavigationDrawer:
            id:navi_draw 
         
            BoxLayout:
                oreintation:'vertical'      
                
               
                   
                MyList:
                    id:list                                 
                                 
                                 
                                 
                
                    
<MyList>:
    oreintation:'vertical'
    padding: "1dp"
    spacing: "1dp"
    AnchorLayout:
        anchor_x: "left"
        size_hint_y: None
        height: avatar.height

    Image:
        id: avatar
        size_hint: None, None
        size: "300dp", "300dp"
        source: "vishu.jpg"
        
        pos_hint:{'center_x':0.5,'center_y':0.9}
    ScrollView:
        MDList:
            OneLineListItem:
                                                                                                        
                                                    
                                                    
                    
<ItemDrawer>:
                                                             
<Submit>:
    name:'submit'
    MDLabel:
        text:'Submitted'
        halign:'center'
        font_style:'H3'
    MDIconButton:
        icon:'arrow-left'
        on_press:
            root.manager.current='home'
            root.manager.transition.direction = 'right'
'''
class MyList(BoxLayout):
    pass 
class ItemDrawer(OneLineListItem):
    pass
class Home(Screen):
    pass
class Submit(Screen):
    pass
sm=ScreenManager()
sm.add_widget(Home(name="home"))
sm.add_widget(Submit(name="submit"))

class MyApp(MDApp):
    def build(self):
        self.theme_cls.primary_palette = "Teal" 

        screen=Screen()
        
        tool=Builder.load_string(KV)
        screen.add_widget(tool)
        return screen                
MyApp().run()       

Tags: textnamefromimportidscreenclasscenter
1条回答
网友
1楼 · 发布于 2024-04-25 13:05:47

Weakly-referenced object no longer exists基本上是在您试图一次又一次地访问类中定义的临时变量时发生的。每个临时变量都有一个上限,最多可以调用多少次,之后它就不存在了。要使一个变量存在得更多,必须在它前面使用self.。因此,只需检查导致错误的部分,并使用self.在该部分中定义变量。另外,还有一件更重要的事情,在早期版本的kivy中有一个bug导致了这个问题。因此,如果您使用的是旧版本,那么首先,请更新它。确保它是kivy和kivymd的最新版本

相关问题 更多 >