Я пытаюсь расширить данные из моего JSON хранилища из моего класса kivy root и отобразить его с помощью recycleview - PullRequest
0 голосов
/ 16 июня 2020

Я пытаюсь создать погодное приложение, которое использует карусель для загрузки трех страниц при запуске приложения. Приложение использует kivy JSONStore для хранения ранее найденных местоположений. Перед запуском приложений необходимо заполнить представление корзины, которое будет загружено каруселью. Проблема возникает, когда я расширяю список из магазина в представление корзины. Это вызывает «AttributeError: объект 'NoneType' не имеет атрибута 'location_list'». Вот соответствующая часть кода:

class WeatherRoot(BoxLayout):
current_weather = ObjectProperty()
locations = ObjectProperty()
forecast = ObjectProperty()
carousel = ObjectProperty()
add_location_form = ObjectProperty()


def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.store = JsonStore('weather_store.json')
    if self.store.exists('locations'):
        locations = self.store.get('locations')
        self.locations.locations_list.data.extend(locations['locations'])            
        current_location = locations['current_location'] 
        self.show_current_weather(current_location)

    else:
        Clock.schedule_once(lambda dt: self.show_add_location_form())        


def show_current_weather(self, location= None):
    if list(location) not in [n['country'] for n in self.locations.locations_list.data]:
        self.locations.locations_list.data.append({'country':[location[0], location[1]]})
        self.store.put('locations', 
            locations= list(self.locations.locations_list.data), 
            current_location= list(location))
    self.current_weather.location = location
    self.forecast.location = location
    self.current_weather.update_weather()
    self.forecast.update_weather()
    self.carousel.load_slide(self.current_weather)

    if self.add_location_form is not None:
        self.add_location_form.dismiss()


def show_add_location_form(self):
    self.add_location_form = AddLocationForm()
    self.add_location_form.open()

А вот соответствующая часть файла kv

WeatherRoot:

<WeatherRoot>:
    carousel: carousel
    locations: locations
    current_weather: current_weather
    forecast: forecast
    Carousel:
        id: carousel
        Locations:
            id: locations
        CurrentWeather:
            id: current_weather
        Forecast:
            id: forecast     
<Locations>:
locations_list: locations_list
BoxLayout:
    orientation: 'vertical'

    RVLocations:
        id: locations_list
        viewclass: 'My_Label'
        RecycleBoxLayout:
            orientation: 'vertical'
            default_size: None, dp(60)
            default_size_hint: 1, None
            size_hint_y: None
            height: self.minimum_height

    BoxLayout:
        orientation: 'horizontal'
        size_hint_y: None
        height: dp(40)
        Button:
            text: 'Add Location'
            on_press: app.root.show_add_location_form()
        Button:
            text: 'Settings'
            on_press: app.open_settings()         

И это трассировка:

    Traceback (most recent call last):
   File "c:/Users/abaomi/Documents/Python Scripts/Personal Projects/using_kivy/weather/main.py", line 280, in <module>
     WeatherApp().run()
   File "C:\Users\abaomi\Anaconda3\lib\site-packages\kivy\app.py", line 828, in run
     self.load_kv(filename=self.kv_file)
   File "C:\Users\abaomi\Anaconda3\lib\site-packages\kivy\app.py", line 599, in load_kv
     root = Builder.load_file(rfilename)
   File "C:\Users\abaomi\Anaconda3\lib\site-packages\kivy\lang\builder.py", line 301, in load_file
     return self.load_string(data, **kwargs)
   File "C:\Users\abaomi\Anaconda3\lib\site-packages\kivy\lang\builder.py", line 399, in load_string
     widget = Factory.get(parser.root.name)(__no_builder=True)
   File "c:/Users/abaomi/Documents/Python Scripts/Personal Projects/using_kivy/weather/main.py", line 222, in __init__
     self.locations.locations_list.data.extend(locations['locations'])
 AttributeError: 'NoneType' object has no attribute 'locations_list'
...