1

I am currently trying to create a weather app in Python using tkinter. I have programmed a button that gets input in the "city" variable. However when I press "search" on the button my "city" variable does not work and returns the name of the city for openweather API to search.

from tkinter import *
import tkinter as tk
from geopy.geocoders import Nominatim
from tkinter import ttk,messagebox
from timezonefinder import TimezoneFinder
from datetime import datetime
from meteostat import Point, Daily
import requests
import pytz

api_key = "secret_api_key"


root=Tk()
root.title("Weather App")
root.geometry("900x500+300+200")
root.resizable(False,False)

#entry box
entry1 = Entry(root, width = 20)
entry1.place(relx=0.5, rely=0.5, anchor=CENTER)

#CITY BUTTON
city = entry1.get()

#search button
btn = Button(root, text="search", command=city)
btn.place(x=450, rely=0.57, anchor=CENTER)

#Title text
label1 = Label(root,text="Welcome to the weather app", font=("Arial", 24))
label1.place(x=300, y=150)

#Enter a location text
label2 = Label(root,text="Enter your city", font=("Arial", 14))
label2.place(x=400, y=200)

#weather graphic
Search_image=PhotoImage(file="/Users/lucky/Downloads/cloudy (1).png")
myimage=Label(image=Search_image)
myimage.place(x=20,y=20)

weather_data = requests.get(
    f"https://api.openweathermap.org/data/2.5/weather?q={city}&units=imperial&APPID={api_key}")

print(weather_data.json())



root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
LSJ32141
  • 49
  • 3
  • hi, perhaps configure a callback to retrieve the value after the mainloop starts, see https://stackoverflow.com/a/43008165/235354 – jspcal Jan 31 '23 at 17:49

1 Answers1

0

First off, never post your API Keys publicly unless you want someone to use up your quotas.

You need to create a function that is called whenever your button is clicked. This is called a "callback" and is usually used to run a snippet of code whenever an event is triggered. In this case, your event is the button press. By moving your API request into that callback, you can make it trigger any time your button is pressed, whereas before, it was only getting called at the start of the script, and thats it. Below you can find the updated code with a callback and your obfuscated api key.

from tkinter import *
import tkinter as tk
from geopy.geocoders import Nominatim
from tkinter import ttk,messagebox
from timezonefinder import TimezoneFinder
from datetime import datetime
from meteostat import Point, Daily
import requests
import pytz

api_key = "API_KEY_HERE"


root=Tk()
root.title("Weather App")
root.geometry("900x500+300+200")
root.resizable(False,False)

#entry box
entry1 = Entry(root, width = 20)
entry1.place(relx=0.5, rely=0.5, anchor=CENTER)

#CITY BUTTON
def get_city_weather():
    city = entry1.get()
    weather_data = requests.get(
    f"https://api.openweathermap.org/data/2.5/weather?q={city}&units=imperial&APPID={api_key}")
    print(weather_data.json())


#search button
btn = Button(root, text="search", command=get_city_weather)
btn.place(x=450, rely=0.57, anchor=CENTER)

#Title text
label1 = Label(root,text="Welcome to the weather app", font=("Arial", 24))
label1.place(x=300, y=150)

#Enter a location text
label2 = Label(root,text="Enter your city", font=("Arial", 14))
label2.place(x=400, y=200)

#weather graphic
Search_image=PhotoImage(file="/Users/lucky/Downloads/cloudy (1).png")
myimage=Label(image=Search_image)
myimage.place(x=20,y=20)

root.mainloop()
MattKA
  • 3
  • 2