From 8327679003cc549b2859714bce44540ba545153b Mon Sep 17 00:00:00 2001 From: Felix Pan Date: Tue, 30 Jun 2020 16:48:37 +0800 Subject: [PATCH 1/2] Add condition display style --- module_kivy/kivy_weather_app/main.py | 18 ++++++- module_kivy/kivy_weather_app/weather.kv | 69 ++++++++++++++++++++++++- 2 files changed, 83 insertions(+), 4 deletions(-) diff --git a/module_kivy/kivy_weather_app/main.py b/module_kivy/kivy_weather_app/main.py index 0623e671..a4c7a8a1 100644 --- a/module_kivy/kivy_weather_app/main.py +++ b/module_kivy/kivy_weather_app/main.py @@ -8,6 +8,7 @@ from kivy.properties import ObjectProperty, BooleanProperty, ListProperty, StringProperty, NumericProperty from kivy.network.urlrequest import UrlRequest from kivy.uix.recycleview.views import RecycleDataViewBehavior +from kivy.factory import Factory WEATHER_APP_ID = '54f64871388416c8ff4a4b198787e8b1' @@ -32,9 +33,13 @@ def show_add_location_form(self): self.add_widget(AddLocationForm()) +class Conditions(BoxLayout): + conditions = StringProperty() + + class CurrentWeather(BoxLayout): location = ListProperty(['New York', 'US']) - conditions = StringProperty() + conditions = ObjectProperty() temp = NumericProperty() temp_min = NumericProperty() temp_max = NumericProperty() @@ -45,11 +50,20 @@ def update_weather(self): request = UrlRequest(weather_url, self.weather_retrieved) def weather_retrieved(self, request, data): - self.conditions = data['weather'][0]['description'] + self.render_conditions(data['weather'][0]['description']) self.temp = data['main']['temp'] self.temp_min = data['main']['temp_min'] self.temp_max = data['main']['temp_max'] + def render_conditions(self, conditions_description): + if 'clear' in conditions_description.lower(): + conditions_widget = Factory.ClearConditions() + else: + conditions_widget = Factory.UnknownConditions() + conditions_widget.conditions = conditions_description + self.conditions.clear_widgets() + self.conditions.add_widget(conditions_widget) + class AddLocationForm(BoxLayout): search_input = ObjectProperty() diff --git a/module_kivy/kivy_weather_app/weather.kv b/module_kivy/kivy_weather_app/weather.kv index ffa7052b..b1193111 100644 --- a/module_kivy/kivy_weather_app/weather.kv +++ b/module_kivy/kivy_weather_app/weather.kv @@ -1,8 +1,71 @@ WeatherRoot: AddLocationForm +: + Label: + text: root.conditions + +: + canvas.before: + Color: + rgb: [0.2, 0.2, 0.2] + Ellipse: + pos: self.pos + size: self.size + +: + canvas.before: + Color: + rgb: [0.8, 0.7, 0.3] + Line: + cap: 'round' + width: 3 + points: + [ + self.center_x - (self.height / 2), + self.center_y, + self.center_x + (self.height / 2), + self.center_y + ] + Line: + cap: 'round' + width: 3 + points: + [ + self.center_x, + self.center_y - (self.height / 2), + self.center_x, + self.center_y + (self.height / 2) + ] + Line: + cap: 'round' + width: 3 + points: + [ + self.center_x - (self.height * .35355), + self.center_y - (self.height * .35355), + self.center_x + (self.height * .35355), + self.center_y + (self.height * .35355) + ] + Line: + cap: 'round' + width: 3 + points: + [ + self.center_x - (self.height * .35355), + self.center_y + (self.height * .35355), + self.center_x + (self.height * .35355), + self.center_y - (self.height * .35355) + ] + Color: + rgb: [0.6, 0.5, 0.0] + Ellipse: + pos: self.center_x - ((self.height - 40) / 2), self.pos[1] + 20 + size: [self.height - 40, self.height - 40] + : orientation: 'vertical' + conditions: conditions BoxLayout: Label: text: '{} ({})'.format(root.location[0], root.location[1]) @@ -10,8 +73,10 @@ WeatherRoot: Label: text: '{}'.format(root.temp) font_size: '30dp' - Label: - text: root.conditions +# Label: +# text: root.conditions + BoxLayout: + id: conditions BoxLayout: orientation: 'horizontal' Label: From ec2234b9bbce442c6dbd6436d0f76d1f038ee9b2 Mon Sep 17 00:00:00 2001 From: Felix Pan Date: Wed, 1 Jul 2020 11:45:20 +0800 Subject: [PATCH 2/2] Add snow effect --- module_kivy/kivy_weather_app/main.py | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/module_kivy/kivy_weather_app/main.py b/module_kivy/kivy_weather_app/main.py index a4c7a8a1..baae5af9 100644 --- a/module_kivy/kivy_weather_app/main.py +++ b/module_kivy/kivy_weather_app/main.py @@ -1,3 +1,4 @@ +import random from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.label import Label @@ -9,6 +10,8 @@ from kivy.network.urlrequest import UrlRequest from kivy.uix.recycleview.views import RecycleDataViewBehavior from kivy.factory import Factory +from kivy.graphics import Color, Ellipse +from kivy.clock import Clock WEATHER_APP_ID = '54f64871388416c8ff4a4b198787e8b1' @@ -37,6 +40,34 @@ class Conditions(BoxLayout): conditions = StringProperty() +class SnowConditions(Conditions): + FLAKE_SIZE = 5 + NUM_FLAKES = 60 + FLAKE_AREA = FLAKE_SIZE * NUM_FLAKES + FLAKE_INTERVAL = 1.0 / 30.0 + + def __init__(self, **kwargs): + super(SnowConditions, self).__init__(**kwargs) + self.flakes = [[x * self.FLAKE_SIZE, 0] for x in range(self.NUM_FLAKES)] + Clock.schedule_interval(self.update_flakes, self.FLAKE_INTERVAL) + + def update_flakes(self, time): + for f in self.flakes: + f[0] += random.choice([-1, 1]) + f[1] -= random.randint(0, self.FLAKE_SIZE) + if f[1] < 0: + f[1] = random.randint(0, int(self.height)) + self.canvas.before.clear() + with self.canvas.before: + widget_x = self.center_x - self.FLAKE_AREA / 2 + widget_y = self.pos[1] + for x_flake, y_flake in self.flakes: + x = widget_x + x_flake + y = widget_y + y_flake + Color(0.9, 0.9, 1.0) + Ellipse(pos=(x, y), size=(self.FLAKE_SIZE, self.FLAKE_SIZE)) + + class CurrentWeather(BoxLayout): location = ListProperty(['New York', 'US']) conditions = ObjectProperty() @@ -58,6 +89,8 @@ def weather_retrieved(self, request, data): def render_conditions(self, conditions_description): if 'clear' in conditions_description.lower(): conditions_widget = Factory.ClearConditions() + elif 'snow' in conditions_description.lower(): + conditions_widget = SnowConditions() else: conditions_widget = Factory.UnknownConditions() conditions_widget.conditions = conditions_description