Avatar of Hiro 714
Hiro 714 asked on

Python Tkinter question

I want to set "Tokyo" permanently and remove text entry.

I would like to update the weather info every 1 hour

Please advise


# Import Required Library
import requests
from tkinter import *
from PIL import Image,ImageTk

# Create Object
root = Tk()

# Set title
root.title('Weather App')

# Set geometry
root.geometry('560x400')

# Set max and min size
root.maxsize(560,300)
root.minsize(560,300)

# weather app text
weather_app = Label(root,text="WEATHER APP",font=("Times", "24", "bold italic"), bg='#87ceeb',pady=5)
weather_app.pack()

# downloading weather condition image
def get_image(url_img,image_name):
    global weather_img
    url = requests.get(f'http:{url_img}')
    with open(image_name,'wb') as f:
        f.write(url.content)
    f.close()
    weather_img = ImageTk.PhotoImage(Image.open(image_name))

# request of weather
def weather_request(location):
    url=requests.get(f'http://api.weatherapi.com/v1/forecast.json?key=b5e01861b575475ca2f34339211003&q={location}&days=7')
    data = url.json()
    return (data['location']['name'],
        data['location']['region'],
        data['current']['temp_c'],
        data['current']['condition']['text'],
        data['current']['wind_kph'],
        data['current']['pressure_mb'],
        data['current']['humidity'],
        data['current']['condition']['icon'])


# get location text
def weather_information(event):

    name,region,temp_c,condtion,wind_kph,pressure_mb,humd,today_weather_img = weather_request(location_text.get())

    # get image of weather
    get_image(today_weather_img,'weather.png')
    
    # set the label text
    weather_condition_image.config(image=weather_img)
    location.config(text=f'{name},{region}')
    weather_condition.config(text=condtion)
    temperature.config(text=int(temp_c))
    degree_celcius.config(text="°C")
    pressure.config(text=f'Pressure: {int(pressure_mb)}hpa')
    humidity.config(text=f'Humidity: {humd}%')
    wind.config(text=f'Wind: {int(wind_kph)} km/h')
    location_text.delete(0,END)

# location entry box 
location_text = Entry(root,font=("Arial", "20"),width=30,borderwidth=2)
location_text.bind('<Return>',weather_information)
location_text.place(x=50,y=50)

# location
location = Label(root,text="",font=("pacifico", "20","italic"),bg='#87ceeb')
location.place(x=10,y=100)

#weather condition
weather_condition=Label(root,text="",font=("Times", "14","italic"),bg='#87ceeb')
weather_condition.place(x=10,y=160)

#weather condition image
weather_condition_image=Label(root,bg='#87ceeb')
weather_condition_image.place(x=20,y=185)

# temperature
temperature=Label(root,text="",font=("Times", "40","italic"),bg='#87ceeb')
temperature.place(x=100,y=185)

# degree_celcius
degree_celcius=Label(root,text="",font=("Times", "14","italic"),bg='#87ceeb')
degree_celcius.place(x=160,y=190)

# pressure
pressure=Label(root,text="",font=("Times", "14","italic"),bg='#87ceeb')
pressure.place(x=350,y=180)

# humidity
humidity=Label(root,text="",font=("Times", "14","italic"),bg='#87ceeb')
humidity.place(x=350,y=207)

# wind
wind=Label(root,text="",font=("Times", "14","italic"),bg='#87ceeb')
wind.place(x=350,y=234)

# loop ends
root.configure(bg='#87ceeb')

# Execute Tkinter
root.mainloop()

Open in new window

Python

Avatar of undefined
Last Comment
David H.H.Lee

8/22/2022 - Mon
David H.H.Lee

I want to set "Tokyo" permanently and remove text entry.
This line
...today_weather_img = weather_request(location_text.get()) 

Open in new window

change to
...today_weather_img = weather_request("Tokyo"

Open in new window


ASKER
Hiro 714

Thank you.
I changed, but how do I run the script?


ASKER CERTIFIED SOLUTION
David H.H.Lee

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes