Welcome folks today in this blog post we will be adding the calendar widget to pick dates using tk_datecalendar library in tkinter All the full source code of the application is shown below.
Get Started
In order to get started you need to install the below library using the pip
command as shown below
pip install tk_datepicker
Now after installing this you nee to make an app.py
file and copy paste the below code
app.py
1 2 3 4 5 6 7 8 9 10 11 |
from tkinter import Tk, Entry, Button from tk_datepicker import Datepicker root = Tk() root.geometry("600x600") entry = Entry() entry.pack() button = Button(text="Click me!") button.pack() dp = Datepicker(entry, button) root.mainloop() |
As you can see first of all we are importing the tk_datepicker library from the datepicker library. And also we are importing all the methods from the tkinter. And in this guys we have two widgets on the screen first is the simple Entry widget which is also equal to input field and then we have a button to pick the date from the datepicker. If you execute the app it will show something like this
As you can see we are using this above datepicker to select the correct date and time for your respective application where you want to store locally
Changing the Theme of the Datepicker
You can also change the theme and the colors of the datepicker widget as shown above.
1 2 3 |
dp = Datepicker(entry, button) dp.ui_settings(days_bg="#888", days_fg="#FFF", t_days_bg="#FFF", t_days_fg="#FFF", back_month_text="<= Back", next_month_text="Next =>") |
As you can see this takes the argument for backgroud color and the foreground play. And also we have the css variables.
Rendering Calendar
Now we will be defining on how to define and render this calendar which is defining the alignment of the calculator as shown below
1 2 3 4 5 6 7 8 |
1) TOP CENTER 2) TOP LEFT 3) TOP RIGHT 4) BOTTOM CENTER 5) BOTTOM LEFT 6) BOTTOM RIGHT 7) RIGHT CENTER 8) LEFT CENTER |