DateEntry widget of ttkbootstrap

Ttkbootstrap DateEntry
Date can be selected from a Calendar and inserted to a Combobox.


Parameters


dateformatThe date format to be returned. Check all Date formats here.
firstweekday0=Monday, 1=Tuesday ... To display on Calendar
startdateDatetime, the date to be infocus, default is current date
bootstyleStyle keyword, options are
primary, secondary, success,info,warning,danger, light, dark
**kwargsOther keyword arguments.


DateEntry of Ttkbootstrap to select date from a Calendar with options and parameters #13


import ttkbootstrap as ttk  
from ttkbootstrap.constants import *  
from datetime import date  

my_w = ttk.Window()  
my_w.geometry("420x300")  # width and height  

dt2 = date(2023, 12, 30)  # start date  

de = ttk.DateEntry(dateformat='%Y-%m-%d', firstweekday=2, startdate=dt2)  
de.grid(row=1, column=1, padx=10, pady=20)  

my_w.mainloop()  

Reading and displaying selected date on a Label on click of a button.

Ttkbootstrap DateEntry get selected date
import ttkbootstrap as ttk  
from ttkbootstrap.constants import *  
from datetime import date  

my_w = ttk.Window()  
my_w.geometry("520x320")  # width and height  

dt2 = date(2023, 12, 30)  # for startdate  
sel = ttk.StringVar()  

de = ttk.DateEntry(dateformat='%Y-%m-%d', firstweekday=2, startdate=dt2)  
de.grid(row=1, column=1, padx=10, pady=20)  

def my_upd():  
    l1.configure(text=de.entry.get())  # displaying date  

b1 = ttk.Button(my_w, text='Show date', command=lambda: my_upd())  
b1.grid(row=1, column=2)  

l1 = ttk.Label(my_w, text='Date')  # to display date here  
l1.grid(row=1, column=3)  

my_w.mainloop()  

Reading and displaying selected date on a Label ( without button click )

The override in MyDateEntry works by redefining the _on_date_ask() method of ttk.DateEntry. When the date picker is opened and a date is selected, the original _on_date_ask() method runs first (super()._on_date_ask()), ensuring normal behavior. Then, the event_generate("<<DateEntrySelected>>") function is triggered, dispatching a virtual event that allows other parts of the program (like the label update function) to respond automatically. This eliminates the need for a button and enables real-time updates whenever a date is chosen.
import ttkbootstrap as ttk  
from ttkbootstrap.constants import *  
from datetime import datetime  

class MyDateEntry(ttk.DateEntry):  
    # Override function  
    def _on_date_ask(self):  
        super()._on_date_ask()  
        # Generate the virtual event  
        self.event_generate("<<DateEntrySelected>>")  

    # Function to return the selected date  
    def get_date(self):  
        return datetime.strptime(self.entry.get(), self._dateformat)  

# Create main window  
window = ttk.Window(themename='darkly')  
window.title('Calendar')  
window.geometry('300x400')  

# Label to display the selected date  
label = ttk.Label(window, text='Date: ')  
label.pack(pady=10)  

# Function to update label when a date is selected  
def update_label(event):  
    print(f'event: {event}')  
    date_object = event.widget.get_date()  
    print(date_object)  
    formatted_date = date_object.strftime("%A, %b %d")  
    print(formatted_date)  

    # Update the label with the selected date  
    label.configure(text=f'Date: {formatted_date}')  

# Use the custom DateEntry widget  
cal = MyDateEntry(window, bootstyle="primary")  
cal.pack()  

# Bind the event to the DateEntry  
cal.bind('<<DateEntrySelected>>', update_label)  

# Run the application  
window.mainloop()  
All ttkbootstrap styles are applied using the bootstyle parameter. We can use all the avilable styles and apply to DateEntry.
DateEntry all bootstyle colours
import ttkbootstrap as ttk  
from ttkbootstrap.constants import *  
from datetime import date  

my_w = ttk.Window()  
my_w.geometry("700x200")  # width and height  

c, r = 0, 0  

for my_style in my_w.style.colors:  # List of styles  
    de = ttk.DateEntry(bootstyle=my_style)  
    de.grid(row=r, column=c, padx=2, pady=20)  

    c = c + 1  
    if c == 4:  
        c, r = 0, 1  

my_w.mainloop()  

state

After creation of the DateEntry we can update the state option. It can take values : readonly, disabled or invalid.
de.configure(state='readonly')
#de.configure(state='disabled')
de.configure(state='invalid')

ttkbootstrap tkcalendar DateEntry
Subhendu Mohapatra — author at plus2net
Subhendu Mohapatra

Author

🎥 Join me live on YouTube

Passionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.



Subscribe to our YouTube Channel here



plus2net.com



29-12-2024

I found a solution so you don't need a button! Override the class and generate the event, since it isn't generated.

import ttkbootstrap as ttk
from ttkbootstrap.constants import *
from ttkbootstrap.widgets import DateEntry
from datetime import datetime

class MyDateEntry(ttk.DateEntry):
# override function
def _on_date_ask(self):
super()._on_date_ask()
# generate the virtual event
self.event_generate("<<DateEntrySelected>>")

# function to return the selected date
def get_date(self):
return datetime.strptime(self.entry.get(), self._dateformat)

window = ttk.Window(themename='darkly')
window.title('Calendar')
window.geometry('300x400')

label = ttk.Label(window, text='Date: ')
label.pack(pady=10)

def update_label(event):
print(f'event: {event}')
date_object = event.widget.get_date()
print(date_object);
formatted_date = date_object.strftime("%A, %b %d")
print(formatted_date)
#global label
label.configure(text=f'Date: {formatted_date}')


# use the custom DateEntry
cal = MyDateEntry(window, bootstyle="primary")
cal.pack()

cal.bind('<<DateEntrySelected>>', update_label)

# run
window.mainloop()

02-02-2025

Thanks,
Included above, thanks again.




Python Video Tutorials
Python SQLite Video Tutorials
Python MySQL Video Tutorials
Python Tkinter Video Tutorials
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer