collinsanele

A simple weather app

Nov 11th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. from kivy.uix.label import Label
  2. from kivy.uix.textinput import TextInput
  3. from kivy.uix.button import Button
  4. from kivy.app import App
  5. from kivy.uix.popup import Popup
  6. from kivy.uix.floatlayout import FloatLayout
  7. from kivy.uix.scrollview import ScrollView
  8. from kivy.metrics import Metrics, dp, sp
  9. import json
  10. import requests
  11.  
  12.  
  13. class Layout(FloatLayout):
  14. def __init__(self, **kwargs):
  15. super().__init__(**kwargs)
  16. self.label_banner = Label(text='Weather app', size_hint=(None,None), width=dp(150),
  17. height=dp(50),
  18. pos_hint={'top':0.98, 'center_x':0.50},
  19. font_size=sp(35))
  20.  
  21. self.label_city = Label(text='City:', size_hint=(None,None), width=dp(50),
  22. height=dp(50),
  23. pos_hint={'top':0.88, 'x':0.06},
  24. font_size=sp(20))
  25.  
  26. self.txtin_city = TextInput(text='', size_hint=(None,None), width=dp(260),
  27. height=dp(50),
  28. pos_hint={'top':0.88, 'x':0.20},
  29. font_size=sp(35))
  30.  
  31. self.btn_fetch = Button(text='Fetch', size_hint=(None,None), width=dp(150),
  32. height=dp(50),
  33. pos_hint={'top':0.74, 'center_x':0.50},
  34. font_size=sp(25), on_release=self.process, on_press=self.wait)
  35.  
  36. self.label_result = Label(text='', size_hint=(None,None), width=dp(350),
  37. height=dp(370),
  38. pos_hint={'top':0.64, 'center_x':0.50},
  39. font_size=sp(20))
  40.  
  41. self.popup_wait = Popup(title='In-Progress', content=Label(text='Please wait'), size_hint=(None, None), width= dp(300),
  42. height=dp(200))
  43.  
  44. self.popup_done = Popup(title='Done', content=Label(text='Done'), size_hint=(None, None), width= dp(300),
  45. height=dp(200))
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56. self.add_widget(self.label_banner)
  57. self.add_widget(self.label_city)
  58. self.add_widget(self.txtin_city)
  59. self.add_widget(self.btn_fetch)
  60. self.add_widget(self.label_result)
  61.  
  62.  
  63.  
  64.  
  65.  
  66. def process(self, *args):
  67. main_api = 'http://api.openweathermap.org/data/2.5/weather?appid=0c42f7f6b53b244c78a418f4f181282a&q='
  68. try:
  69. url = main_api + self.txtin_city.text + '&units=metric'
  70. r = requests.get(url).json()
  71. temp = r['main']['temp']
  72. temp_min = r['main']['temp_min']
  73. temp_max = r['main']['temp_max']
  74. humidity = r['main']['humidity']
  75.  
  76. except Exception:
  77. e = 'Error try again'
  78. self.label_result.text = e
  79.  
  80. else:
  81.  
  82. 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'])
  83. self.label_result.text = info
  84. self.popup_done.open()
  85.  
  86. def wait(self, *args):
  87. self.popup_wait.open()
  88.  
  89.  
  90.  
  91.  
  92. class Main(App):
  93. def build(self, *args):
  94. return Layout()
  95.  
  96.  
  97. app = Main()
  98. app.run()
Advertisement
Add Comment
Please, Sign In to add comment