I'm still making my weather app and I need a function to change the City or Country The problem is that I have to restart the program to display the changes but when I do restart- The default city gets loaded instead of the new one I have tried many ways to fix this but they all didn't work Thanks in advance!
# !/usr/bin/python3
#Please don't use my API-KEY for bad purposes, I have only included it to help run the code
import requests, json
from tkinter import *
import os
CITY = "Glasgow"
BASE_URL = "https://api.openweathermap.org/data/2.5/weather?"
URL = "https://api.openweathermap.org/data/2.5/weather?q=+" + CITY + "&units=metric&APPID=confedential"
response = requests.get(URL)
def func() :
def change():
y = Toplevel()
y.geometry("200x100")
en = Entry(y, width=10)
en.place(x=25, y=25)
en.focus()
def getr(e):
def restart():
x.destroy()
func()
CITY = en.get()
restart()
en.bind("<Return>", getr)
if response.status_code == 200:
data = response.json()
main = data['main']
temperature = main['temp']
humidity = main['humidity']
pressure = main['pressure']
report = data['weather']
print(f"{CITY:-^30}")
print(f"Temperature: {temperature}")
print(f"Humidity: {humidity}")
print(f"Pressure: {pressure}")
print(f"Weather Report: {report[0]['description']}")
rep = report[0]['main'].lower()
if "clear" in rep:
image = 'images/sunny.png'
if "cloud" in rep:
image = 'images/cloud.png'
if "rain" in rep:
image = 'images/rain.png'
if "thunder" in rep:
image = 'images/thunder.png'
if "mist" in rep:
image = 'images/mist.png'
if "snow" in rep:
image = 'images/snow.png'
x = Tk()
# Creating Menubar
menubar = Menu(x)
menubar.config(bg="#484848", fg="white", font=("Stencil Std", 10))
# Adding Help Menu
help_ = Menu(menubar, tearoff=0, bg="#484848", fg="white", font=("Stencil Std", 10))
menubar.add_cascade(label='Countries', menu=help_)
help_.add_command(label='Change Current Country', command=change)
help_.add_command(label='Show Current Country', command=None)
help_.add_separator()
help_.add_command(label='Change Timezone', command=None)
help_.add_command(label='Show Current Timezone', command=None)
help_.add_separator()
help_.add_command(label="Exit", command=x.destroy)
# display Menu
x.config(menu=menubar)
x.resizable(False, False)
gif = PhotoImage(file=image)
cvwid = gif.width()
cvhei = gif.height()
canvas = Canvas(x, width=cvwid, height=cvhei, bg='lightblue')
canvas.pack(fill=BOTH)
img = canvas.create_image(0, 0, image=gif, anchor=NW)
temp = canvas.create_text(cvwid / 2, 350, fill="White", font="Helvetica 30", text=str(int(temperature)) + "°C")
reportr = canvas.create_text(cvwid / 2, 400, fill="White", font="Helvetica 20", text=report[0]["main"])
x.title(f"{CITY:-^30}")
x.mainloop()
func()
###The problem is that you assignresponse = requests.get(URL)
only once. No matter how often you conly once。 No matter how often you change the city the response that was established with the parameter"Glasgow"
will always stay the same.
The fastest fix without refactoring the whole codebase would be to make CITY and response global so you can modify them from within a function。 For example:
CITY = "Glasgow"
URL = "https://api.openweathermap.org/data/2.5/weather?q=+{}&units=metric&APPID=confidential"
def get_response(city=CITY):
return requests.get(URL.format(city))
response = get_response()
Then you could global your variables in your getr(e) function like this。
def getr(e):
global CITY, response
CITY = en.get()
response = get_response(CITY)
restart()
This is a solution to the problem you currently have。 However I would suggest to go back to your codebase。 With a good refactor you would not need this: You could think about seperating logic and ui make your functions accept parameters and seperate them from each other research how to update/change display values within a running Tk() mainloop instead of destroying and recalling it。 Maybe it would help to post your code on stackexchange for codereview。