DEV Community

Ali Hassan
Ali Hassan

Posted on

Weather Dashboard (CLI)

from kivy.app import App from kivy.uix.label import Label from kivy.uix.boxlayout import BoxLayout from kivy.uix.textinput import TextInput from kivy.core.window import Window from kivy.uix.button import Button from kivy.uix.floatlayout import FloatLayout from kivy.uix.image import Image import requests Window.clearcolor = (1, 1, 1, 1) class Weather(App): def build(self): self.root = FloatLayout() # Background image self.background = Image(source='default.jpg', allow_stretch=True, keep_ratio=False) self.root.add_widget(self.background) # BoxLayout for UI elements self.boxlayout = BoxLayout(orientation="vertical", spacing=20, padding=40, size_hint=(0.9, 0.9), pos_hint={"center_x": 0.5, "center_y": 0.5}) # Widgets self.label = Label(text="Weather App", color=(0, 0, 0, 1), bold=True, font_size=34) self.cityname = TextInput(hint_text="Enter City Name", size_hint_y=None, height=40) self.button = Button(text="Check Weather", size_hint_y=None, height=40, size_hint_x=None, width=200, pos_hint={"center_x": 0.5}, on_press=self.buttonpressed) self.label2 = Label(text="", color=(1,1,1, 1), italic=True, font_size=34) self.label3 = Label(text="", color=(1,1,1, 1), italic=True, font_size=34) self.label4 = Label(text="", color=(1,1,1, 1), italic=True, font_size=34) self.label5 = Label(text="", color=(1,1,1, 1), italic=True, font_size=34) self.label6 = Label(text="", color=(1,1,1, 1), italic=True, font_size=34) self.label7 = Label(text="", color=(1,1,1, 1), italic=True, font_size=34) self.label8 = Label(text="", color=(1,1,1, 1), italic=True, font_size=34) # Add widgets to layout self.boxlayout.add_widget(self.label) self.boxlayout.add_widget(self.cityname) self.boxlayout.add_widget(self.button) self.boxlayout.add_widget(self.label2) self.boxlayout.add_widget(self.label3) self.boxlayout.add_widget(self.label4) self.boxlayout.add_widget(self.label5) self.boxlayout.add_widget(self.label6) self.boxlayout.add_widget(self.label7) self.boxlayout.add_widget(self.label8) self.root.add_widget(self.boxlayout) return self.root def buttonpressed(self, instance): api_key = "a0e1e3df147647ebad9110730251105" city = self.cityname.text.strip() if not city: self.label2.text = "Please enter a city name" return url = f"http://api.weatherapi.com/v1/current.json?key={api_key}&q={city}" try: response = requests.get(url) if response.status_code == 200: data = response.json() condition = data["current"]["condition"]["text"].lower() # Change background image based on condition if "sunny" in condition: self.background.source = "sunny.jpg" elif "rain" in condition or "drizzle" in condition: self.background.source = "rainy.jpg" elif "cloud" in condition or "overcast" in condition: self.background.source = "cloudy.jpg" else: self.background.source = "default.jpg" self.background.reload() # Refresh the image # Set weather details self.label2.text = f"City Name : {data['location']['name']}" self.label3.text = f"Region : {data['location']['region']}" self.label4.text = f"Country : {data['location']['country']}" self.label5.text = f"Condition : {data['current']['condition']['text']}" self.label6.text = f"Wind Speed : {data['current']['wind_kph']} kph" self.label7.text = f"Feels Like : {data['current']['feelslike_c']} °C" self.label8.text = f"Temperature : {data['current']['temp_c']} °C" else: self.label2.text = "Error fetching weather data" except Exception as e: self.label2.text = f"Error: {str(e)}" if __name__ == "__main__": Weather().run() 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)