Create a GUI to Get Sunset and Sunrise Time using Python


What is Python and what are its advantages?

Python is a high-level programming language that is used for a wide range of applications, including web development, scientific computing, data analysis, and artificial intelligence. It has a simple and elegant syntax that makes it easy to read and write, and a large standard library that provides many useful tools and functions. Some of the advantages of Python include its readability, ease of use, flexibility, and portability. It is an open-source language, which means that it is freely available and can be modified and distributed by anyone. Python also has a large and active community of developers who contribute to its development and provide support and resources to other users.

What are the key advantages of using Tkinter?

Tkinter is a Python library used for creating GUI applications. Here are some of the key advantages of using Tkinter −

  • It is included in Python − Tkinter is included in the standard Python distribution, which means that it is always available to use without installing any additional libraries.

  • Cross-platform compatibility − Tkinter works on all major operating systems, including Windows, macOS, and Linux.

  • Easy to learn − Tkinter has a simple and intuitive API, which makes it easy to learn and use, especially for beginners who are just starting with GUI programming.

  • Customizable widgets − Tkinter provides a wide range of customizable widgets, such as buttons, labels, text boxes, check boxes, radio buttons, list boxes, and more. These widgets can be configured to suit your specific application requirements.

  • Easy to create complex layouts − Tkinter provides several geometry managers (pack, grid, and place) that help in arranging widgets on the screen in a flexible manner.

  • High-level of control over user interfaces − Tkinter provides a high level of control over the look and feel of the user interface, allowing you to customize the appearance of widgets, fonts, colors, and more.

  • Support for event-driven programming − Tkinter is based on an event-driven programming model, which means that it is designed to respond to user events, such as mouse clicks, button presses, and keyboard input. This makes it easy to create interactive applications.

Overall, Tkinter is a versatile and easy-to-use GUI toolkit that can help you create professional-looking desktop applications with minimal effort.

Prerequisites

Before we dive into the details of creating a GUI, you should have a basic understanding of Python programming, object-oriented programming (OOP) concepts, and how to work with the Tkinter module.

List of recommended settings

  • pip install tkinter, astral

  • It is expected that the user will have access to any standalone IDE such as VS-Code, PyCharm, Atom or Sublime text.

  • Even online Python compilers can also be used such as Kaggle.com, Google Cloud platform or any other will do.

  • Updated version of Python. At the time of writing the article I have used 3.10.9 version.

  • Knowledge of the use of Jupyter notebook.

  • Knowledge and application of virtual environment would be beneficial but not required.

Step 1: Installation

To begin, we need to make sure that we have all necessary libraries installed in our system to support our project. We'll be using Python's Tkinter library to create the GUI, and we'll also need the Astral library to obtain the sunset and sunrise times.

To install the Tkinter library astral, we can use pip with the following command −

pip install python-tk
pip install astral

Step 2: Importing Libraries

Once we have the libraries installed, we can import them into our Python script.

from tkinter import *
from astral.sun import sun
from astral import LocationInfo
from datetime import datetime

Step 3: Creating the GUI

Now, we can start building our GUI by defining the window's size and title. In this example, we'll create a simple window with a label and a button.

# Creating the GUI
window = Tk()
window.geometry("500x300")
window.title("Sunset and Sunrise Time")

label = Label(window, text="")
label.pack()

button = Button(window, text="Get Time", command=get_time)
button.pack()

window.mainloop()

Step 4: Adding Functionality

Our GUI is now set up, but it doesn't do anything yet. We'll add a function called `get_time` to get the current location's sunset and sunrise time.

# Adding Functionality
def get_time():
   city = LocationInfo("New York", "USA")
   s = sun(city.observer, date=datetime.now(), tzinfo=city.timezone)
   sunrise = s['sunrise'].strftime('%H:%M:%S')
   sunset = s['sunset'].strftime('%H:%M:%S')
   label.config(text=f"Sunrise: {sunrise}\nSunset: {sunset}")

In this function, we first define our location, which in this example is New York. We then use the `sun` function from the `astral.sun` library to obtain the sunrise and sunset times for our location. We format the times using `strftime` and finally update the label in our GUI with the times using the `config` method.

Step 5: Testing the Application

We can now run the application and test it out! Run the script by typing `python filename.py` in the terminal or running it from within an IDE.

After running the script, the GUI window will appear. Click the "Get Time" button, and the sunrise and sunset times for New York will be displayed.

Final Program, Code

# Importing Libraries
from tkinter import *
from astral.sun import sun
from astral import LocationInfo
from datetime import datetime

# Adding Functionality
def get_time():
   city = LocationInfo("New York", "USA")
   s = sun(city.observer, date=datetime.now(), tzinfo=city.timezone)
   sunrise = s['sunrise'].strftime('%H:%M:%S')
   sunset = s['sunset'].strftime('%H:%M:%S')
   label.config(text=f"Sunrise: {sunrise}\nSunset: {sunset}")

# Creating the GUI
window = Tk()
window.geometry("500x300")
window.title("Sunset and Sunrise Time")

label = Label(window, text="")
label.pack()

button = Button(window, text="Get Time", command=get_time)
button.pack()

window.mainloop()

Output

Once the button is clicked we get the timings

This section shows the output as desired.

Conclusion

In this guide, we have walked through creating a graphical user interface (gui) to obtain the sunset and sunrise time using python. We have used the tkinter library to create the gui and the astral library to obtain the sunset and sunrise times. The code we've written can be modified and used for various applications needing sunrise and sunset time information across the globe.

Additionally, we've also seen how we can use the `strftime` function to format variables to display them in a readable format. With this knowledge, you can go on to modify this code further to get sunrise and sunset time for any location in the world, integrate it into other systems, and much more!

Updated on: 20-Apr-2023

264 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements