2

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()
j_4321
  • 15,431
  • 3
  • 34
  • 61
Garima Tiwari
  • 1,490
  • 6
  • 28
  • 46
  • [tkcalendar](https://github.com/j4321/tkcalendar/releases) now has a `showweeknumbers` option (since version 1.3.0) – j_4321 Sep 27 '18 at 10:54

3 Answers3

3

The following seems to work to turn the weeknumbers off:

self.de = DateEntry(self.frame, width=12, background='darkblue',
                foreground='white',showweeknumbers=False,firstweekday='sunday')

# To see other parameters that you can change
print("DE keys=",self.de.keys())

Documentation for weeknumbers

newcool
  • 319
  • 2
  • 20
Larry Jens
  • 31
  • 1
2

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

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
  • Hacky but works. I didn't dare recommending to temper with the internal attributes of `Calendar`! – Jacques Gaudin Jun 05 '18 at 12:15
  • 1
    In fact I must admit this *hack* is against good code practices (manipulating a private attribute) and what you said in your answer is correct. I think the best thing in this situation is to make a function for this purpose and pull request it and hope to be accepted. Nevertheless, what you said in your answer is correct (*it is always added*) +1 @JacquesGaudin – Billal Begueradj Jun 05 '18 at 12:20
  • @BillalBEGUERADJ : Works like a charm! Although can you explain why the same thing will not work for the DateEntry widget? because as is quoted in multiple places - any option for Calendar should work on DateEntry as well. – Garima Tiwari Jun 05 '18 at 13:35
1

Unfortunately, it doesn't come as an option. In the __init__ method of tkcalendar the week numbers are always added, no matter what the options are:

    ...
    self._week_nbs = []
    self._calendar = []
    for i in range(1, 7):
        self._cal_frame.rowconfigure(i, weight=1)
        wlabel = ttk.Label(self._cal_frame, style='headers.%s.TLabel' % self._style_prefixe,
                           font=self._font, padding=2,
                           anchor="e", width=2)
        self._week_nbs.append(wlabel)
        wlabel.grid(row=i, column=0, sticky="esnw", padx=(0, 1))
        self._calendar.append([])
        for j in range(1, 8):
            label = ttk.Label(self._cal_frame, style='normal.%s.TLabel' % self._style_prefixe,
                              font=self._font, anchor="center")
            self._calendar[-1].append(label)
            label.grid(row=i, column=j, padx=(0, 1), pady=(0, 1), sticky="nsew")
            if selectmode is "day":
                label.bind("<1>", self._on_click)

You may want to try other widgets as suggested here, it seems like ttkcalendar wouldn't have week numbers.

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75