Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from kivy.uix.label import Label
- from kivy.uix.textinput import TextInput
- from kivy.uix.button import Button
- from kivy.app import App
- from kivy.uix.popup import Popup
- from kivy.uix.floatlayout import FloatLayout
- from kivy.uix.scrollview import ScrollView
- from kivy.metrics import Metrics, dp, sp
- import json
- import requests
- class Layout(FloatLayout):
- def __init__(self, **kwargs):
- super().__init__(**kwargs)
- self.label_banner = Label(text='Weather app', size_hint=(None,None), width=dp(150),
- height=dp(50),
- pos_hint={'top':0.98, 'center_x':0.50},
- font_size=sp(35))
- self.label_city = Label(text='City:', size_hint=(None,None), width=dp(50),
- height=dp(50),
- pos_hint={'top':0.88, 'x':0.06},
- font_size=sp(20))
- self.txtin_city = TextInput(text='', size_hint=(None,None), width=dp(260),
- height=dp(50),
- pos_hint={'top':0.88, 'x':0.20},
- font_size=sp(35))
- self.btn_fetch = Button(text='Fetch', size_hint=(None,None), width=dp(150),
- height=dp(50),
- pos_hint={'top':0.74, 'center_x':0.50},
- font_size=sp(25), on_release=self.process, on_press=self.wait)
- self.label_result = Label(text='', size_hint=(None,None), width=dp(350),
- height=dp(370),
- pos_hint={'top':0.64, 'center_x':0.50},
- font_size=sp(20))
- self.popup_wait = Popup(title='In-Progress', content=Label(text='Please wait'), size_hint=(None, None), width= dp(300),
- height=dp(200))
- self.popup_done = Popup(title='Done', content=Label(text='Done'), size_hint=(None, None), width= dp(300),
- height=dp(200))
- self.add_widget(self.label_banner)
- self.add_widget(self.label_city)
- self.add_widget(self.txtin_city)
- self.add_widget(self.btn_fetch)
- self.add_widget(self.label_result)
- def process(self, *args):
- main_api = 'http://api.openweathermap.org/data/2.5/weather?appid=0c42f7f6b53b244c78a418f4f181282a&q='
- try:
- url = main_api + self.txtin_city.text + '&units=metric'
- r = requests.get(url).json()
- temp = r['main']['temp']
- temp_min = r['main']['temp_min']
- temp_max = r['main']['temp_max']
- humidity = r['main']['humidity']
- except Exception:
- e = 'Error try again'
- self.label_result.text = e
- else:
- info = 'Name: ' + str(r['name']) + 2*'\n' + 'Temp:' + str(temp) +2*'\n' + 'temp_min:' + str(temp_min) + 2*'\n' + 'temp_max:' + str(temp_max) + 2*'\n' + 'humidity:' + str(humidity) + 2*'\n' + 'Pressure:' + str(r['main']['pressure']) + 2*'\n' + 'Description: ' + str(r['weather'][0]['description'])
- self.label_result.text = info
- self.popup_done.open()
- def wait(self, *args):
- self.popup_wait.open()
- class Main(App):
- def build(self, *args):
- return Layout()
- app = Main()
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment