How to Create Very Beautiful Weather Showing app Using Python For Windows, Linux and Mac OS | python web scraping | python gui | python example

Posted by Suraj Singh on January 14, 2017 · 20 mins read
hii readers,

                     This Tutorial Is About How We Can Create Weather App Using Python And Tkinter. First of all, I want to clear that this is only for educational and practice purpose only. That's Why I will Not take any type of responsibility for this app.

So, let's Start,

But First, Here, I am Sharing Some Screen Shots of My App:





looking cool! hahaha, I know.

Requirements:

           1. Tkinter (Built-In) 
           2. BeautifulSoup (Use "pip install BeautifulSoup" For Automatically Installation)


Features:

          1. Cross-Platform Support 
          2. Good Internet Connection Handling System  
          3. Automatic Features Auto-Show and Auto-Hide GUI Widget

So, Let's Start, 

     For This Project, I am Using MVC Technique (Model, View, Control).

So, For This Example. first I Created 3 Files

        1. weather
        2. view
        3. main

Let Me Explain You What Are The Functions of these files,

Here,
               Weather Script Is For Connecting To Internet and Scraping All Data And Providing Useful Data in Dictionary.

  The view is For GUI and Main is For Controlling Both Script and Other Controlling Systems.

Looking Hard! hmmm,

don't worry, this project is really very easy. :)

1. weather => for Downloading weather-related data.


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#
# Suraj
# surajsinghbisht054@gmail.com
# www.bitforestinfo.com
#
#
# Return Data Format : Dictionary
#
# Example
# 'next summary' : u' Clear throughout the\xa0day. ',
# 'temperature' : u'21\u02da',
# 'Humidity:' : u'25%',
# 'Rain' : u'0mm',
# 'Visibility:' : u'10+km',
# 'Wind:' : u'5kph\u2191',
# 'UV Index:' : u'8',
# 'Pressure:' : u'1016hPa',
# 'summary' : u'Clear.',
# 'Dew Pt:' : u'0\u02da'
#
#
# Site For Scraping >> This Url Is For Indian Temperature
site='https://darksky.net/forecast/22.3511,78.6677/ca12/en'

# Importing Modules
import urllib2
import bs4
import re

# Creating Class For Data Handling
class DataExtraction:
def __init__(self, site):
self.site=site

# Creating Function For HTML Code Retrive
def create_page_handler(self):
try:
# Retreive html Page
html=urllib2.urlopen(self.site)
except:
print "Please Check Your Internet Connection!"
html=None
if html:
# Feeding Html Data Into BeautifulSoup
self.page=bs4.BeautifulSoup(html.read(),'html.parser')
else:
self.page=None
return

# Creating Function For Extracting Data
def extract_data_from_page(self):

# Content Class Configuration
labels =self.page.findAll(class_='label swip')
labelvalue =self.page.findAll(class_='val swap')

self.ExtractedValues={} # Dictionary For Storing Data

# Checking Data Is Ready For Extracting Automatically
if len(labels)==len(labelvalue):
for i in xrange(len(labels)):
self.ExtractedValues[labels[i].text.replace('\n','')]=labelvalue[i].get_text().replace('\n','')

self.ExtractedValues['temperature'] = self.page.find('span',{'class':'temp swip'}).get_text().replace('\n','')
self.ExtractedValues['summary'] = self.page.find('span',{'class':'summary swap'}).get_text().replace('\n','')
self.ExtractedValues['next summary'] = self.page.find('span',{'class':'next swap'}).get_text().replace('\n','')
return self.ExtractedValues
return

# Trigger of All Function
def start(self):
self.create_page_handler()
if self.page:
return self.extract_data_from_page()



# Testing Function
if __name__=='__main__':
handler=DataExtraction(site)
print handler.start()

2. View => for controlling GUI (Graphical User Interface)


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#
# Suraj
# surajsinghbisht054@gmail.com
# www.bitforestinfo.com
#
#
# Importing Modules
try:
import Tkinter
except:
import tkinter as Tkinter
import weather
import time


# Configurations
WIDTH=300 # Set Width Of Widget
HEIGHT=300 # Set Height
LOOP_TURN=100 # Update Loop Turns
LOOP_WAIT=0.1 # Time For Waiting On Every Loop Turn

class GUI(Tkinter.Tk):
def __init__(self, DATAVALUE):
Tkinter.Tk.__init__(self)
self.DATAVALUE=DATAVALUE
self.main_function_handler()

# Creating Function for Handling Other Functions
def main_function_handler(self):
self.creating_function_for_appereance()
self.coordinate_position()
self.creating_canvas_board()
self.creating_Gui_valiadilty()
return

# Creating Function For Showing Data
def creating_canvas_board(self):
canvas=Tkinter.Canvas(self,bg='skyblue')
canvas.create_text(50,10,text=u''.join(self.DATAVALUE.pop('temperature')), font=('arial 100 bold'),anchor='nw', fill='gray5') # Temperature
canvas.create_text(130,150,text=u''.join(self.DATAVALUE.pop('next summary').upper()), font=('arial 10 bold'),fill='Blue') # Next Summary
for i,j in enumerate(self.DATAVALUE.iteritems()):
num=i
(label,value)=j
canvas.create_text(50,180+(20*i),text=u'{}'.format(label.upper()), font=('arial 10 italic'),anchor='nw', fill='gray10')
canvas.create_text(150,180+(20*i),text=u'{}'.format(value), font=('arial 10 italic'),anchor='nw', fill='gray20')
canvas.pack(expand='yes',fill='both')
return

# Close Function
def close_widget(self):
self.destroy()
return

# Function For Handling GUI Apperance Style
def creating_function_for_appereance(self):
self.focus_force()
self['bg']='gray'
self.overrideredirect(True)
return

# Function Fon Handling GUI Position
def coordinate_position(self):
self.geometry("%dx%d+%d+%d" % (WIDTH,HEIGHT,\
self.winfo_screenwidth()/1.5-(WIDTH/1.5),\
self.winfo_screenheight()/1.5-(HEIGHT),\
))

# Function For Handling GUI Loop Timing
def creating_Gui_valiadilty(self):
x=1.0
for i in range(LOOP_TURN):
time.sleep(LOOP_WAIT)
self.update_idletasks()
self.update()
self.attributes('-alpha',x)
x=x-0.01
self.destroy()
return


# Main Trigger
if __name__ == '__main__':
# This Data Is only For Testing GUI
DATAVALUE={'next summary': u' Clear throughout the\xa0day. ', 'temperature': u'21\u02da', u'Humidity:': u'25%', u'Rain': u'0mm', u'Visibility:': u'10+km', u'Wind:': u'5kph\u2191', u'UV Index:': u'8', u'Pressure:': u'1016hPa', 'summary': u'Clear.', u'Dew Pt:': u'0\u02da'}
# Starting GUI Function
GUI(DATAVALUE)

3. Main => Main Controller


 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
#
# Suraj
# surajsinghbisht054@gmail.com
# www.bitforestinfo.com
#
#
# Configuration
TIME_LAP= 3600 # In Seconds
ERROR_TIME_LAP = 10 # Seconds
RETRY=15

# Site For Scraping >> This Url Is For Indian Temperature
site='https://darksky.net/forecast/22.3511,78.6677/ca12/en'

# Importing Module
import time
import weather
import view
import sys

# Creating Loop
while True:
retry=0 # Retry Counting
data=weather.DataExtraction(site) # Here Retriving Data Handler
try:
while True:
k=data.start() # Connecting To Website
if k:
# Showing Our GUI
view.GUI(k)
else:
print "RETRY"
time.sleep(ERROR_TIME_LAP)
retry=retry+1 # Counting Every Retry
if retry==RETRY: # Checking Retry Times
print "Break"
sys.exit(0)

except Exception as e:
print e

Done!



if anyone wants to download then  Click here


thanks for visiting my blog.


For any question or Suggestion or help
Suraj
Email:  surajsinghbisht054@gmail.com
Or post Comment.
please feel free to contact me.