【问题标题】:Reference error:Weakly referenced objects no longer exists error in KivyMd when using OneLineListItem引用错误:使用 OneLineListItem 时 KivyMd 中的弱引用对象不再存在错误
【发布时间】:2021-01-23 20:52:04
【问题描述】:

当我运行使用 kivymd 制作的应用程序时,它在我使用 OneLineListItem 时显示错误,当我删除它时,应用程序运行顺利。我使用的是 Python 版本 3.8。当我放置 OneLineListItem 时在 MyList Block 中的 ScrollView、MDList 之后,它给出了错误。当我编写另一个只有带有 onelinelistitem 的 mdlist 的代码时,它正在运行,请解决这个问题。 日志是这样写的-

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()       

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

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

    【讨论】:

    • 但是我没有使用变量,我在滚动视图下使用一个行列表项小部件,它显示错误,当我删除滚动视图行时它可以,但列表没有出现跨度>
    • 我还说kivymd的早期版本有一个bug,所以要确保kivy和kivymd都是最新的
    猜你喜欢
    • 2020-08-22
    • 2020-12-01
    • 2018-08-28
    • 2016-10-01
    • 1970-01-01
    • 2010-09-17
    • 2020-07-19
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多