0

From what I've read, this is a big no-no, but my current code is an eye sore, so I was hoping to lean on your expertise to see if there's a best practice way of initializing similar variables. Right now, this is how my code looks and seems ideal for a loop (for i in range(6)):

If you think a loop would suffice for this, how do I make a variable name in that loop? I need to have them as a text name (I think) as opposed to storing it all in a dictionary for when I update these images in the kv file. I say that because I don't know how to call a dictionary in the kv file that is defined in the Python code. Any clever ideas?

        WeatherForecastImage:
            id: forecast_img_1 
            source: root.forecast_img_1

This is in the Python portion of my kivy build out:

forecast_img_1 = ObjectProperty(all_des.get(forecasts[1].text().lower()))
forecast_img_2 = ObjectProperty(all_des.get(forecasts[2].text().lower()))
forecast_img_3 = ObjectProperty(all_des.get(forecasts[3].text().lower()))
forecast_img_4 = ObjectProperty(all_des.get(forecasts[4].text().lower()))
forecast_img_5 = ObjectProperty(all_des.get(forecasts[5].text().lower()))
user6142489
  • 512
  • 2
  • 10
  • 28
  • 2
    If you want to collect multiple values, you should use a `dict` – user1767754 Nov 27 '17 at 18:47
  • 2
    A dictionary is indeed the right data structure. Since your variable names seem to be an enumeration, you could also consider a list `forecast_img` which you then index into (starting at zero). – timgeb Nov 27 '17 at 18:48

0 Answers0