I am practicing API integration into my some of my projects and I wanted to try and practice some dictionaries.
Pulled weather data and long story short: trying to create simple tkinter app that will show me things from this dictionary.
import tkinter as tk
window = tk.Tk()
sample = {"location": {"name": "Belgrade", "region": "Central Serbia", "country": "Serbia", "lat": 44.8, "lon": 20.47, "tz_id": "Europe/Belgrade", "localtime_epoch": 1643798038, "localtime": "2022-02-02 11:33"}, "current": {"last_updated_epoch": 1643796900, "last_updated": "2022-02-02 11:15", "temp_c": 2.0, "temp_f": 35.6, "is_day": 1, "condition": {"text": "Sunny", "icon": "//cdn.weatherapi.com/weather/64x64/day/113.png", "code": 1000}, "wind_mph": 0.0, "wind_kph": 0.0, "wind_degree": 228, "wind_dir": "SW", "pressure_mb": 1009.0, "pressure_in": 29.8, "precip_mm": 0.0, "precip_in": 0.0, "humidity": 100, "cloud": 0, "feelslike_c": -1.4, "feelslike_f": 29.6, "vis_km": 10.0, "vis_miles": 6.0, "uv": 1.0, "gust_mph": 9.2, "gust_kph": 14.8, "air_quality": {"co": 317.1000061035156, "no2": 13.399999618530273, "o3": 45.400001525878906, "so2": 10.5, "pm2_5": 11.399999618530273, "pm10": 15.600000381469727, "us-epa-index": 1, "gb-defra-index": 1}}}
for j, k in sample['location'].items():
tk.Label(window, text=j).grid(column=0)
tk.Label(window, text=k).grid(column=1)
window.mainloop()
However, when I make it I am getting weird thing: key: value pairs are added in zig-zag order, not in one next to other.
For example:
Location of ‘name’ key is (0,0). However value of it is put at (1, 1).
I could put all in single column and than it would get sort-of sorted and readable, however, I would love to understand why is output as is.
Strange pattern
>Solution :
You can set row, using a variable.
I have called it row
in this example:
import tkinter as tk
window = tk.Tk()
sample = {"location": {"name": "Belgrade", "region": "Central Serbia", "country": "Serbia", "lat": 44.8, "lon": 20.47, "tz_id": "Europe/Belgrade", "localtime_epoch": 1643798038, "localtime": "2022-02-02 11:33"}, "current": {"last_updated_epoch": 1643796900, "last_updated": "2022-02-02 11:15", "temp_c": 2.0, "temp_f": 35.6, "is_day": 1, "condition": {"text": "Sunny", "icon": "//cdn.weatherapi.com/weather/64x64/day/113.png", "code": 1000}, "wind_mph": 0.0, "wind_kph": 0.0, "wind_degree": 228, "wind_dir": "SW", "pressure_mb": 1009.0, "pressure_in": 29.8, "precip_mm": 0.0, "precip_in": 0.0, "humidity": 100, "cloud": 0, "feelslike_c": -1.4, "feelslike_f": 29.6, "vis_km": 10.0, "vis_miles": 6.0, "uv": 1.0, "gust_mph": 9.2, "gust_kph": 14.8, "air_quality": {"co": 317.1000061035156, "no2": 13.399999618530273, "o3": 45.400001525878906, "so2": 10.5, "pm2_5": 11.399999618530273, "pm10": 15.600000381469727, "us-epa-index": 1, "gb-defra-index": 1}}}
row = 0
for j, k in sample['location'].items():
tk.Label(window, text=j).grid(column=0,row=row)
tk.Label(window, text=k).grid(column=1,row=row)
row += 1
window.mainloop()