in __init__ word=self.search_box.text AttributeError: 'NoneType' object has no attribute 'text'

后端 未结 1 1571
挽巷
挽巷 2021-01-28 02:33

I am making an application.as far as I know I am doing thingd correctly but still getting this error

word=self.search_box.text AttributeError: \'NoneT

相关标签:
1条回答
  • 2021-01-28 02:40

    the problem is caused because the children are not stuck in the constructor of the parent, it does it an instant later so search_box will be None in the constructor, the solution is to execute it an instant after finishing the constructor with the help of Clock:

    from kivy.clock import Clock
    
    
    class DisplayPage(Screen):
        search_box= ObjectProperty()
        label_maening=StringProperty()
        label_synonym=StringProperty()
        label_ant=StringProperty()
        label_sentence=StringProperty()
    
    
        def __init__(self, **kwargs):
            super(DisplayPage,self).__init__(**kwargs)
            Clock.schedule_once(self.callback)
    
        def callback(self, dt):
            with open('vocab_words.json') as rfile:
                data=json.load(rfile)
    
            word=self.search_box.text               #the error occurred here 
    
            for value in data:
                if value['word']==word:
                    self.label_maening=value['meaning']
                    self.label_synonym=value['synonym']
                    self.label_ant=value['antonyms']
                    self.label_sentence=value['sentence']
    
    0 讨论(0)
提交回复
热议问题