Rory LM
Rory LM

Reputation: 180

Tkcalendar configure DateEntry widget

Is there any way to configure the DateEntry widget itself as you would with a normal entry widget (not the drop down calendar)? For example change the font, relief or background etc...

I've tried the following:

myDateEntry.config(background='red')

but i just get:

AttributeError: 'DateEntry' object has no attribute 'background'

When i define the DateEntry widget i can do the following:

myDateEntry=DateEntry(root,background='red')

which doesn't cause an error, but only changes the background of the drop down calendar.

Upvotes: 0

Views: 3545

Answers (1)

j_4321
j_4321

Reputation: 16179

The DateEntry widget is based on a ttk.Entry, not a tk.Entry, so you have to use a style to change its appearance. This is explained in the documentation: https://tkcalendar.readthedocs.io/en/stable/howtos.html#widget-styling

Like for a ttk.Entry, if you want a red background, you need to set the fieldbackground option of the style to 'red', except that the style to change is 'DateEntry' instead of 'TEntry':

import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry

root = tk.Tk()
style = ttk.Style(root)
# create custom DateEntry style with red background
style.configure('my.DateEntry', fieldbackground='red')
# create DateEntry using the custom style
dateentry = DateEntry(root, style='my.DateEntry') 
dateentry.pack()

root.mainloop()

Note: Not all ttk themes allow to change the fieldbackground of the widgets, especially Windows default theme. To be able to change it therefore the theme needs to be changed first with style.theme_use('clam') to use, for instance, the 'clam' theme.

Upvotes: 2

Related Questions