正在删除 Python 日历小部件中的 'Week Number' 列

Removing 'Week Number' column in Calendar Widget in Python

我在 Python 中有一个来自 tkcalendar 的简单日历和 DateEntry 小部件,它工作正常并显示如下附件的结果。

希望它在日历中显示周数列[或带有下个月星期的额外底行] - 我尝试使用“showWeeks = False" 但它似乎并不能解决问题。

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

我知道日历小部件的任何自定义选项也适用于 DateEntry 小部件,因此我们将不胜感激任何线索,需要我能得到的所有建议。谢谢!

下面的日历和 DateEntry 代码:

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()

不幸的是,它不是一个选项。在tkcalendar__init__方法中week numbers are always added,不管选项是什么:

    ...
    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)

您可能想尝试建议的其他小部件here, it seems like ttkcalendar 不会有周数。

文档很差,你必须在 source code 中挖掘。

周数列对应于名为 _week_nbswlabel 列表(ttk.Label() 的实例)。所以你可以遍历它们并一个接一个地销毁它们:

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

完整节目:

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()

演示:

以下似乎可以关闭周数:

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