[Python] Keyerror, Typeerror and MORE

Discussão em 'Python' iniciado por Stack, Agosto 22, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to make a small desktop app and slowly expand upon it but starting with time/date and current weather but I keep getting a key error when i try to call the api and take specific data using json.

    I've left out my api key but in my code its there. The window opens up, i see the submit button and search bar and can type in it but i always get a keyerror or type error

    I'm not good at defining functions yet so I'm sure that part is incorrect too.

    I keep getting a KeyError when trying to parse just temp and humidity from the api call, on the openweather website it says using cityname is deprecated so i switched to zip code thinking that would solve the KeyError which i dont prefer but it doesn't work either

    Exception in Tkinter callback
    Traceback (most recent call last):
    File "C:\Python3\Lib\tkinter\__init__.py", line 2068, in __call__
    return self.func(*args)
    ~~~~~~~~~^^^^^^^
    File "c:\Python Test Projects\game window test.py", line 30, in get_weather
    on_submit_click = city.json()["main"][0]
    ~~~~~~~~~~~^^^^^^^^
    KeyError: 'main'


    So i'd like help defining the function so i can use it in my tkinter window and work when i click the submit button to search for weather, temp and humidity in the city you input in the search bar.

    from tkinter import *
    import tkinter as tk
    import requests
    import json
    import time
    from PIL import Image, ImageTk
    from tkinter import messagebox, ttk
    from pathlib import Path

    #Program Window
    win = tk.Tk(className="Sprout")
    win.config(bg='#87CEEB')
    #win.overrideredirect(True)

    #Anchoring window to bottom right
    screen_width = win.winfo_screenwidth()
    screen_height = win.winfo_screenheight()
    window_width = 450
    window_height = 450
    x = screen_width - window_width
    y = screen_height - window_height
    win.geometry('450x450')

    #api grab and key for OpenWeather
    key = ''


    #On Button Click
    def get_weather():
    on_submit_click = city.json()["main"][0]
    on_submit_click = city.json()["main"]['temp']
    on_submit_click = city.json()["main"]['humidity']


    #Close Button
    close_button = tk.Button(win, text="Close", command=win.destroy)
    close_button.pack(anchor=SE, pady=10, padx=10)

    #images
    iconPath = Image.open("C:\\Python Project Visual Assets\\leaf png.png")
    resizedIcon = iconPath.resize((32, 32), Image.Resampling.LANCZOS)
    tkIcon = ImageTk.PhotoImage(resizedIcon)
    win.iconphoto(True, tkIcon)

    #Search Bar
    #img = Image.open('C:\\Python Project Visual Assets\\search bar png.png')
    #resizedBar = img.resize((200, 150), Image.Resampling.LANCZOS)
    #imgBar = ImageTk.PhotoImage(resizedBar)
    weather_entry = tk.Entry(win, width = 30, text="Enter Zip Code: ")
    weather_entry.place(x=115, y=305)


    city = requests.get(f"https://api.openweathermap.org/data/2.5/forecast? zip={weather_entry}&appi={key}")

    #Sumbit Button
    btn = tk.Button(win, text="Submit", command=get_weather)
    btn.place(x=178, y=345)

    #Search Results
    searchResults= tk.Label(win, width=45, height=10, bg='#87CEEB')
    searchResults.place(x=65, y=120)


    #time and date
    local_time = time.ctime(time.time())
    time_date = tk.Label(win, text=f"{local_time}", bg='#87CEEB', font=('Impact, 16'))
    time_date.place(x=10, y=8)


    win.mainloop()`

    Continue reading...

Compartilhe esta Página