Develop Your Own Calendar To Track Important Dates with Python

https://miro.medium.com/max/1200/0*QUZPrCzpAU1AsTcq

Original Source Here

Developing Your Calendar:

Screenshot by Author

As we can notice from the above image, the project will require us to have some utility of Graphics User Interface (GUI) for constructing a visually appealing calendar. We will utilize the Tkinter library that is available in python for constructing such a user interface. For learning more about these GUI tools, I would recommend checking out one of my previous articles that explains seven of these tools with starter codes to get you quickly started.

One of the major installations that we will require alongside the Tkinter GUI module is an additional install for tkcalendar that provides the Calendar and Date entry widgets for the Tkinter interface. It allows the user for custom control over the different widgets to manipulate the date and time accordingly as required by the user. The command below should allow you to install the following module with ease.

pip install tkcalendar

Once we have successfully installed the essential libraries, we can now proceed to import them accordingly. The asterisk (‘*’) symbol while making imports means that we are importing all the classes of the Tkinter module as we will require a few of them for successfully computing this project. From the Tkinter Calendar library, we will only require the Calendar module through which we can display the interactive Calendar with its respective widgets.

# Importing The Essential Libraries
from tkinter import *
from tkcalendar import Calendar

Once we complete importing our required libraries, we will proceed to create our GUI object as the root for the display interface. We can then choose our geometry settings accordingly as per the user’s choice. I am setting my dimensions for the interface as 700 x 700 because I like to use a bigger interactable screen for most tasks. We will then add the Calendar module that we recently installed as an additional widget for Tkinter GUI.

With this calendar module, we can set the root path, the particular day, month, and year, as required. While packing this variable, I will apply padding on the y-axis so that there is a distance of 20 pixels from the top of the interactive screen as well as a distance of 20 pixels from the place where we create a button or label or any other type of interactive widget. We will fill both sides and expand the variable to the end of the screen so that we can have a more prolific view of the calendar. The code for this process is as shown in the below code snippet.

# Create The Gui Object
tk = Tk()

# Set the geometry of the GUI Interface
tk.geometry("700x700")

# Add the Calendar module
cal = Calendar(tk, selectmode = 'day',
year = 2022, month = 1,
day = 11)

cal.pack(pady = 20, fill="both", expand=True)

In the next code snippet, we will create a function through which we can grab the particular date that is required by the user. The grad date function contains the text that will be displayed once we click on one of our button widgets. If you want to add more commands to the following function, then feel free to explore it further as desired.

# Function to grab the selected date
def grad_date():
date.config(text = "Selected Date is: " + cal.get_date())

Finally, we will create some of the few requirements for our project to select the appropriate date and time. Firstly, we will create a button that is linked through our function to grab the particular date that we choose with our mouse cursor. We will then add a label that will display the text on the screen with the specific date when the button is clicked. A padding operation of 20 pixels is applied to maintain equivalent distance for the label and the button. We will then execute the Tkinter loop for our calendar project.

# Adding the Button and Label
Button(tk, text = "Get Date",
command = grad_date).pack(pady = 20)

date = Label(tk, text = "")
date.pack(pady = 20)

# Execute Tkinter
tk.mainloop()

Once you have executed the following code snippets perfectly, you can proceed to run the Python program to receive a result similar to the image displayed above. Let us move to the next section to explore the complete code as well as discuss some of the improvements that we can add to make this project even better.

AI/ML

Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot

%d bloggers like this: