Home GUI weather page and button problems (python)

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()


`

New contributor

Jake Rumberger is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật