Bu kodda da farklı bir siteden farklı bir şekilde textbox ile şehir girişi sağlanılıp " Api " verisine ihtiyaç duyulmadan o şehre ait hava durumu bilgisini çekmektedir.

Kod:
import requests
import tkinter as tk
import tkinter.ttk as ttk
def get_weather(city):
try:
Şehir adını içeren URL'yi oluştur
url = f"https://wttr.in/{city.replace(' ', '+')}?format=3"
# URL'ye istek gönder ve sayfayı al
response = requests.get(url)
response.raise_for_status()
# Sayfa içeriğini al
weather_data = response.text.strip()
if weather_data:
# Hava durumu bilgisini treeview'a ekle
treeview.insert("", "end", values=(city, weather_data))
else:
print("Hava durumu bilgisi alınamadı.")
except requests.exceptions.RequestException:
print("Hava durumu bilgisi alınamadı.")
def on_button_click():
Kullanıcıdan şehir adını al
city = entry.get()
# Hava durumu bilgisini al ve treeview'a ekle
get_weather(city)
root = tk.Tk()
root.title("Hava Durumu")
Şehir giriş etiketi ve alanı
label = tk.Label(root, text="Şehir:")
label.grid(row=0, column=0, sticky="w")
entry = tk.Entry(root)
entry.grid(row=0, column=1, padx=5)
Bilgileri al butonu
button = tk.Button(root, text="Bilgileri Al", command=on_button_click)
button.grid(row=1, column=0, columnspan=2, pady=10)
Treeview (veri tablosu)
treeview = ttk.Treeview(root)
treeview["columns"] = ("City", "Weather")
treeview.heading("City", text="Şehir")
treeview.heading("Weather", text="Hava Durumu")
treeview.grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky="nsew")
Grid yöntemi ile boyut ayarları
root.grid_rowconfigure(2, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
root.mainloop()