Tkinter-Simple Drop Down Calendar and Date Picker

DROP DOWN CALENDAR AND DATE PICKER USING TKINTER

Tkinter, an acronym for Tk interface is the Python library that provided a tool kit for Graphical User Interface(GUI) in Python. It is usually shipped with Python, which means if you have Python installed in your system, you likely already have Tkinter with you.

Tkinter is widely used to develop desktop based GUI applications and even make them responsive. Since it comes with Python, we don’t need to install it separately or go for any other alternatives.It follows an event driven mechanism. Which means, you do or click something, and a certain action is triggered in the window.

Let us see how to use Tkinter and how we can create a calendar with Tkinter.Before that there are some pre-requisites you need to satisfy.

Check out this post to know how to create a calculator using tkinter.

Install Python

As we discussed above, the Tkinter library is shipped along with python. So in order to use tkinter, we need to first install Python in our system.

Follow the below steps and install python.

Go to your browser and search for Python download. Find the download option and download the suitable version for you operating system.The current version of python is 3.11 so make sure you download that.

After downloading, you will find a screen asking for the download instructions. Click on customize installation and also make sure to check the Add python.exe to PATH.

Customize Installation
Customize Installation

Then, you will see another window with all the optional features that will be installed. Make sure that the tcl/tk check box is checked.

Optional Features
Optional Features

Click Next.

Then you will see another prompt asking for the path. Click on browse and specify the path you want python to be installed in.

Install Python
Install Python

You have successfully installed python. Just to be sure, let us check if we have done everything correctly. Open you command prompt and type python --version.

Command Prompt
Command Prompt

As you can see we have installed python correctly.

Now let us check if we have Tkinter.

In the command prompt, run the following command- python -m tkinter. If the tkinter is successfully shipped with python, you will see a small pop-up on your screen that looks something like this .

Tkinter
Tkinter

The second step is to install or set up a Python IDE. You can go for PyCharm , VSCode, Genie, or any other IDE. In this tutorial, we are going to use VSCode.

How to Create a Drop Down and Date Picker Calendar Using Tkinter?

With the help of Tkinter, we can create a pretty simple calendar that has a drop down functionality and it also returns a date you click on.

Refer to this post to know how to change the font settings of the tkinter object.

let us see the code for it.

from tkinter import Tk,Button
from tkcalendar import DateEntry
def get_date():
    selected_date = cal.get()
    print(f"Selected date: {selected_date}")
root = Tk()
cal = DateEntry(root, date_pattern="yyyy-mm-dd")
cal.pack()
btn = Button(root, text="Click Here To Return a Date ", command=get_date)
btn.pack()
root.mainloop()

In the first line, we are importing the Tool Kit and Button classes from the tkinter library.

In the next line, we are importing the DateEntry class from the library.

We are creating a function called get_date that returns a date you select to the console.

The selected_date is the variable we assigned to the date to return. The cal.get() function returns the date from the calendar widget.

The print function is used to display the date we clicked on the console.

The root variable is used to create an instance of the Tkinter class.This is the main window of the application.

The cal object is used to specify a pattern for the date in the calendar.

The pack method is used to make an object appear on the window. If we do not use the pack method, the object will not be displayed on the window.

Next, we are initializing a button object for selecting the date. We also make it responsive. The text on the button is going to be – Click Here To Return a Date. We are making this button visible by using the pack method.

The mainloop is the method used to keep the window active until it is closed.

When we run this code, a pop up appears with a current date and a button below it.

Date Picker
Date Picker

If you click the drop down arrow beside the date, a simple calendar appears.

Calendar
Calendar

Coming to returning a date, you can select any date from the drop down calendar and click the button. You will see the date you selected in the console of your IDE.

Selected Date
Selected Date

As you can see, the date we selected in the window appears on the console.

Conclusion

Tkinter is the most widely used GUI tool kit that is directly shipped with Python during its installation. With Tkinter, we can do many things- build a calculator, create a random facts generator and so on.

We have seen the installation of Python as a pre-requisite to using Tkinter. After we installed Python in our system, we made sure to check the version we downloaded using the command- python --version. We also checked if the GUI tool is available in our system to use with the command- python -m tkinter.

Next, we saw the code to creating a drop down calendar and also a date picker. The code when executed, displays the current date with a drop down arrow beside it and a button that allows us to select a date to return.

The date we select in the calendar will be displayed in the console of your IDE.

References

Know more about the tkinter library from here.

Also refer to the stack overflow answer chain on the same topic.