Create a GUI to get Sunset and Sunrise time using Python. - GeeksforGeeks

19 Dec, 2021
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
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 modulesimport datetimefrom suntime import Sunfrom geopy.geocoders import Nominatim # Nominatim API to get latitude and longitudegeolocator = Nominatim(user_agent="geoapiExercises") # input placeplace = "delhi"location = geolocator.geocode(place) # latitude and longitude fetchlatitude = location.latitudelongitude = location.longitudesun = Sun(latitude, longitude) # date in your machine's local time zonetime_zone = datetime.date(2020, 9,13)sun_rise = sun.get_local_sunrise_time(time_zone)sun_dusk = sun.get_local_sunset_time(time_zone) # displayprint("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
Python3
# import modulesfrom tkinter import *from geopy.geocoders import Nominatimimport datetimefrom suntime import Sun # user defined functiondef 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 greymaster = Tk()master.configure(bg='light grey')master.title("Sun") # Variable Classes in tkinterresult1 = StringVar();result2 = StringVar();  # Creating label for each information# name using widget LabelLabel(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 EntryLabel(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:
avtarkumar719
sweetyty
Python Tkinter-exercises
Python-tkinter
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
How to drop one or multiple columns in Pandas Dataframe
Python Classes and Objects
Python | os.path.join() method
Create a directory in Python
Python | Get unique values from a list
Python | Pandas dataframe.groupby()
Defaultdict in Python