tkcalendar: Change output configuration for DateEntry()

3.2k Views Asked by At

I am using the tkcalendar module that I just downloaded and would like to change the ~result~ background color and position if possible. The code I found on line is below;

try:
    import tkinter as tk
    from tkinter import ttk
except ImportError:
    import Tkinter as tk
    import ttk

from tkcalendar import Calendar, DateEntry

def example1():
    def print_sel():
        print(cal.selection_get())

    top = tk.Toplevel(root)

    cal = Calendar(top, font="Arial 14", selectmode='day', locale='en_US',
                   cursor="hand1", year=2018, month=2, day=5)

    cal.pack(fill="both", expand=True)
    ttk.Button(top, text="ok", command=print_sel).pack()


def example2():

    top = tk.Toplevel(root)

    cal = Calendar(top, selectmode='none')
    date = cal.datetime.today() + cal.timedelta(days=2)
    cal.calevent_create(date, 'Hello World', 'message')
    cal.calevent_create(date, 'Reminder 2', 'reminder')
    cal.calevent_create(date + cal.timedelta(days=-2), 'Reminder 1', 'reminder')
    cal.calevent_create(date + cal.timedelta(days=3), 'Message', 'message')

    cal.tag_config('reminder', background='red', foreground='yellow')

    cal.pack(fill="both", expand=True)
    ttk.Label(top, text="Hover over the events.").pack()


def example3():
    top = tk.Toplevel(root)

    ttk.Label(top, text='Choose date').pack(padx=10, pady=10)

    cal = DateEntry(top, width=12, background='darkblue',
                    foreground='white', borderwidth=2, year=2018)
    cal.pack(padx=10, pady=10)





root = tk.Tk()
ttk.Button(root, text='Calendar', command=example1).pack(padx=10, pady=10)
ttk.Button(root, text='Calendar with events', command=example2).pack(padx=10, pady=10)
ttk.Button(root, text='DateEntry', command=example3).pack(padx=10, pady=10)

root.mainloop()

This actually runs fine. I see the main root window below;

screenshot 1

Now, when I select "DateEntry", once again everything is fine. I get the calendar pop up and I can select a date. But when I'm done selecting my date, I get the graphic below;

screenshot 2

I would like to change the background color on the date and center the 8/29/18 in the center. I have not found any way to accomplish this. Any help would be appreciated.

~OldManEd

1

There are 1 best solutions below

0
On

DateEntry inherits from the ttk.Entry widget, so what you want can be done exactly the same way as with a ttk.Entry:

  • The position of the text inside the entry is controlled by the justify option:

    dateentry.configure(justify='center')
    
  • The color of the background can be changed via the fieldbackground option of a ttk style:

    style = ttk.Style(root)
    style.configure('my.DateEntry', fieldbackground='red')
    dateentry.configure(style='my.DateEntry')