Search code examples
pythonpython-3.xtkinterdatepickertkcalendar

Removing 'Week Number' column in Calendar Widget in Python


I have a simple Calendar and DateEntry widget from tkcalendar in Python which is working fine and displaying results as attached below.

I do not want it to display the Week Number column in the calendar [or the extra bottom row with the next month's week] - I tried using "showWeeks = False" but it does not seem to do the trick.

review_date_entry = DateEntry(dates_panel, 
                              textvariable=self.review_date,
                              showWeeks = False)

I understand that any customization options for Calendar widget work for DateEntry widget as well therefore any leads will be appreciated, need all the suggestions I can get. Thank you!

Code for Calendar and DateEntry 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',
                   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)

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

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

root = tk.Tk()
s = ttk.Style(root)
s.theme_use('clam')

ttk.Button(root, text='Calendar', command=example1).pack(padx=10, pady=10)
ttk.Button(root, text='DateEntry', command=example2).pack(padx=10, pady=10)

root.mainloop()

Solution

  • The documentation is poor, you have to dig in the source code.

    The weeks number column corresponds to a list of wlabel (instance of ttk.Label()) called _week_nbs. So you can loop over them and destroy them one after one:

    for i in range(6):
        cal._week_nbs[i].destroy()
    

    Full program:

    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',
                       cursor="hand1", year=2018, month=2, day=5)
        cal.pack(fill="both", expand=True)
        for i in range(6):
           cal._week_nbs[i].destroy()
        ttk.Button(top, text="ok", command=print_sel).pack()
    
    def example2():
        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)
        cal.pack(padx=10, pady=10)
    
    root = tk.Tk()
    s = ttk.Style(root)
    s.theme_use('clam')
    
    ttk.Button(root, text='Calendar', command=example1).pack(padx=10, pady=10)
    ttk.Button(root, text='DateEntry', command=example2).pack(padx=10, pady=10)
    
    root.mainloop()
    

    Demo:

    enter image description here