So I am making a GUI for my smart home, it has 8 buttons on it that open other pages. The page I am currently working on is my “weather” page. I have the weather updating code and api setup correct. However, my button does nothing now. But when the button works the weather page is blank.
I have tried everything imaginable, I have double, triple and quadruple checked my main page and everything. I will attach my whole code and highlight the part that is controlling that page.
`import tkinter as tk
from datetime import datetime
import requests
# Weather app info
API_KEY = 'already have'
BASE_URL = 'https://api.openweathermap.org/data/2.5/weather'
class MainApp(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.title("Smart Home GUI")
container = tk.Frame(self)
container.grid(row=0, column=0, sticky="nsew")
self.frames = {}
for F in (MissionControl, Google, Music, Media, Security, Lights, FishTank, Garden, Weather):
page_name = F.__name__
frame = F(container, self)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
# Create Weather frame and add it to frames dictionary
self.weather_page = Weather(container, self)
self.frames['Weather'] = self.weather_page
self.show_page("MissionControl")
# Time
self.time_label = tk.Label(self, text="", font=("Arial", 24))
self.time_label.grid(row=6, column=4, columnspan=2, pady=(50, 10), sticky="ew")
# Date
self.date_label = tk.Label(self, text="", font=("Arial", 12))
self.date_label.grid(row=6, column=4, columnspan=2, pady=(0, 10), sticky="ew")
# Music Preview
self.music_label = tk.Label(self, text="Music Preview", font=("Arial", 12))
self.music_label.grid(row=100, column=100, columnspan=2, sticky="sw", padx=10, pady=10)
# Update time and date
self.update_time()
self.update_date()
self.update_weather("Wadsworth")
def show_page(self, page_name):
frame = self.frames[page_name]
frame.tkraise()
def update_time(self):
current_time = datetime.now().strftime("%H:%M:%S")
self.time_label.config(text=current_time)
self.after(1000, self.update_time) # Schedule next update after 1 second
def update_date(self):
current_date = datetime.now().strftime("%Y-%m-%d")
self.date_label.config(text=current_date)
self.after(60 * 1000, self.update_date) # Schedule next update after 1 hour
def update_weather(self, city):
self.weather_page.update_weather(city)
class MissionControl(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Mission Control")
label.grid(row=0, column=0, columnspan=4, pady=10)
# Add buttons here
button_texts = ["Weather", "Music", "Google", "Media", "Security", "Lights", "Garden", "FishTank"]
for i, text in enumerate(button_texts):
row_num = i // 4 + 1 # Determine row number
col_num = i % 4 # Determine column number
button = tk.Button(self, text=f"{text}", command=lambda t=text: controller.show_page(t))
button.grid(row=row_num, column=col_num, padx=5, pady=5)
class Music(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Music")
label.grid(row=0, column=0, pady=10)
button = tk.Button(self, text="Back", command=lambda: controller.show_page("MissionControl"))
button.grid(row=1, column=0, pady=10)
class FishTank(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Fish Tank")
label.grid(row=0, column=0, pady=10)
button = tk.Button(self, text="Back", command=lambda: controller.show_page("MissionControl"))
button.grid(row=1, column=0, pady=10)
class Garden(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Garden")
label.grid(row=0, column=0, pady=10)
button = tk.Button(self, text="Back", command=lambda: controller.show_page("MissionControl"))
button.grid(row=1, column=0, pady=10)
class Google(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Google")
label.grid(row=0, column=0, pady=10)
button = tk.Button(self, text="Back", command=lambda: controller.show_page("MissionControl"))
button.grid(row=1, column=0, pady=10)
class Media(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Media")
label.grid(row=0, column=0, pady=10)
button = tk.Button(self, text="Back", command=lambda: controller.show_page("MissionControl"))
button.grid(row=1, column=0, pady=10)
class Security(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Security")
label.grid(row=0, column=0, pady=10)
button = tk.Button(self, text="Back", command=lambda: controller.show_page("MissionControl"))
button.grid(row=1, column=0, pady=10)
class Lights(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
label = tk.Label(self, text="Lights")
label.grid(row=0, column=0, pady=10)
button = tk.Button(self, text="Back", command=lambda: controller.show_page("MissionControl"))
button.grid(row=1, column=0, pady=10)
class Weather(tk.Frame):
def init(self, parent, controller):
super().init(parent)
self.controller = controller
self.label = tk.Label(self, text="Weather")
self.label.grid(row=1, column=0, pady=10)
self.button = tk.Button(self, text="Back", command=lambda: controller.show_page("MissionControl"))
self.button.grid(row=2, column=0, pady=10)
def update_weather(self, city):
weather_data = get_weather(city)
if weather_data:
self.label.config(text=f"Weather in {city}:n"
f"Temperature: {weather_data['temperature']}°Fn"
f"Description: {weather_data['description']}n"
f"Humidity: {weather_data['humidity']}%n"
f"Wind Speed: {weather_data['wind_speed']} m/s")
else:
self.label.config(text="Failed to fetch Weather data.")
def get_weather(city):
params = {
‘q’: city,
‘appid’: API_KEY,
‘units’: ‘imperial’ # Get temperature in F
}
response = requests.get(BASE_URL, params=params)
if response.status_code == 200:
data = response.json()
weather = {
‘temperature’: data[‘main’][‘temp’],
‘description’: data[‘weather’][0][‘description’],
‘humidity’: data[‘main’][‘humidity’],
‘wind_speed’: data[‘wind’][‘speed’]
}
return weather
else:
print(f”Failed to fetch weather data. Status code: {response.status_code}”)
return None
if __name__ == "__main__":
app = MainApp()
app.mainloop()
`
Jake Rumberger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.