Prerequisite: Tkinter module
In this article, we are going to write python scripts to get sunset and sunrise time and bind it with the GUI application. Before starting we need to understand suntime and geopy module
Modules Needed:
suntime module: This module will return the sunset and sunrise time calculation python library. Run this command in your terminal for installation.
pip install suntime
geopy module: This module locates the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources. Nominatim is a geocoder that is used for OpenStreetMap data. Run this command in your terminal for installation.
pip install geopy
Approach:
- Import all the required modules.
- Get latitude and longitude with geopy.
- Get the Sunrise time with Sun(latitude, longitude).get_sunrise_time() method.
- Get the dusk time with Sun(latitude, longitude).get_sunset_time() method.
Below is the Implementation:
Python3
# import required modules import datetime from suntime import Sun from geopy.geocoders import Nominatim # Nominatim API to get latitude and longitude geolocator = Nominatim(user_agent = "geoapiExercises" ) # input place place = "delhi" location = geolocator.geocode(place) # latitude and longitude fetch latitude = location.latitude longitude = location.longitude sun = Sun(latitude, longitude) # date in your machine's local time zone time_zone = datetime.date( 2020 , 9 , 13 ) sun_rise = sun.get_local_sunrise_time(time_zone) sun_dusk = sun.get_local_sunset_time(time_zone) # display print ( "Sun rise at : " , sun_rise.strftime( '%H:%M' )) print ( "Dusk at : " ,sun_dusk.strftime( '%H:%M' )) |
Output:
Sun rise at : 06:05 Dusk at : 18:28
Binding with GUI application
Python3
# import modules from tkinter import * from geopy.geocoders import Nominatim import datetime from suntime import Sun # user defined function def sun(): try : geolocator = Nominatim(user_agent = "geoapiExercises" ) ladd1 = str (e.get()) location1 = geolocator.geocode(ladd1) latitude = location1.latitude longitude = location1.longitude sun = Sun(latitude, longitude) time_zone = datetime.datetime.now() sun_rise = sun.get_local_sunrise_time(time_zone) sun_dusk = sun.get_local_sunset_time(time_zone) res_rise = sun_rise.strftime( '%H:%M' ) res_dusk = sun_dusk.strftime( '%H:%M' ) result1. set (res_rise) result2. set (res_dusk) except : result1. set ( "oops! something get wrong" ) # object of tkinter # and background set to light grey master = Tk() master.configure(bg = 'light grey' ) master.title( "Sun" ) # Variable Classes in tkinter result1 = StringVar(); result2 = StringVar(); # Creating label for each information # name using widget Label Label(master, text = "Enter place : " , bg = "light grey" ).grid(row = 1 , sticky = W) Label(master, text = "Sunrise :" , bg = "light grey" ).grid(row = 3 , sticky = W) Label(master, text = "Dusk :" , bg = "light grey" ).grid(row = 4 , sticky = W) # Creating label for class variable # name using widget Entry Label(master, text = "", textvariable = result1, bg = "light grey" ).grid(row = 3 ,column = 1 , sticky = W) Label(master, text = "", textvariable = result2, bg = "light grey" ).grid(row = 4 ,column = 1 , sticky = W) e = Entry(master,width = 50 ) e.grid(row = 1 , column = 1 ) # creating a button using the widget b = Button(master, text = "Check" , command = sun, bg = "white" ) b.grid(row = 1 , column = 2 ,columnspan = 2 , rowspan = 2 ,padx = 5 , pady = 5 ,) mainloop() |
Output: