Prerequisite: Tkinter
Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. It is a standard Python interface to the Tk GUI toolkit shipped with Python. Python with Tkinter is the fastest and easiest way to create GUI applications. In this article, we will learn how to create a date picker calendar in Tkinter.
In Tkinter, there is no in-built method for date picker calendar, here we will use the tkcalendar Module.
tkcalendar: tkcalendar is a Python module that provides the Calendar and DateEntry widgets for Tkinter.
For installation run this command into your terminal:
pip install tkcalendar
Approach:
- First, we will import the required library
- Then we will create a Calendar Object and pass the default date
- Pick the year, month, and date from the calendar
- For getting the value of the picked date value, use the get() method.
Syntax: Calendar(master=None, **kw)
year: intCode block
- it initially displayed year, default is the current year.
month: int
- initially displayed month, default is the current month.
day: int
- initially selected day, if month or year is given but not day, no initial selection, otherwise, default is today.
Below is the Implementation:-
Python3
# Import Required Library from tkinter import * from tkcalendar import Calendar # Create Object root = Tk() # Set geometry root.geometry( "400x400" ) # Add Calendar cal = Calendar(root, selectmode = 'day' , year = 2020 , month = 5 , day = 22 ) cal.pack(pady = 20 ) def grad_date(): date.config(text = "Selected Date is: " + cal.get_date()) # Add Button and Label Button(root, text = "Get Date" , command = grad_date).pack(pady = 20 ) date = Label(root, text = "") date.pack(pady = 20 ) # Execute Tkinter root.mainloop() |
Output: