首页 > 解决方案 > 如何在 tkcalendar 中获取单元格的背景?

问题描述

我有这个日历,我想获取所选单元格的背景颜色,我该怎么做?

Current_Date = str(datetime.today())
Current_Date=Current_Date[0:10].split('-')
cal = Calendar(root, selectmode="day", year=int(Current_Date[0]), 
               month=int(Current_Date[1]), day=int(Current_Date[2]))
cal.pack(pady=20)

标签: pythontkintertkcalendar

解决方案


坦率地说,我不知道您为什么需要访问单元格以及为什么需要了解单元格的背景。如果你想改变单元格的颜色,那么你应该使用标签`

顺便说一句:您不必将日期转换为字符串并将其拆分

current_date = datetime.datetime.today()

cal = tkcalendar.Calendar(root, selectmode="day",
                   year=current_date.year, 
                   month=current_date.month,
                   day=current_date.day)

tkcalendar不能直接访问单元格。但是隐藏cal._calendar了所有用于创建单元格的标签 - 这样您就可以尝试访问您需要的标签。但是 Label 不直接使用,background但是style您必须将样式转换为免费颜色。


此代码显示当月所有单元格的样式

import datetime
import tkinter as tk
import tkcalendar

# --- functions

def on_click():
    #print(cal._calendar)

    # display all `labels` 
    for row in cal._calendar:
        for day in row:
            number = day['text']
            style = day['style']
            background = cal.style.configure(day['style'])['background']
            print('day: {:2} | style: {:27} | background: {}'.format(number, style, background))
            
# --- main ---

root = tk.Tk()

current_date = datetime.datetime.today()  # PEP8: lower_case_namas

cal = tkcalendar.Calendar(root, selectmode="day",
                   year=current_date.year, 
                   month=current_date.month,
                   day=current_date.day)
cal.pack(pady=20, padx=20)
               
button = tk.Button(root, text='Show all styles', command=on_click)
button.pack(pady=(0,20), padx=20)

print(cal.style)
root.mainloop()

结果:

day: 30 | style: normal_om..!calendar.TLabel | background: gray93
day: 1  | style: normal..!calendar.TLabel    | background: white
day: 2  | style: normal..!calendar.TLabel    | background: white
day: 3  | style: normal..!calendar.TLabel    | background: white
day: 4  | style: normal..!calendar.TLabel    | background: white
day: 5  | style: we..!calendar.TLabel        | background: gray80
day: 6  | style: we..!calendar.TLabel        | background: gray80
day: 7  | style: normal..!calendar.TLabel    | background: white
day: 8  | style: normal..!calendar.TLabel    | background: white
day: 9  | style: normal..!calendar.TLabel    | background: white
day: 10 | style: normal..!calendar.TLabel    | background: white
[...]

如果您想在单击单元格时访问,那么您可能必须创建自己的类并覆盖方法_on_click

import datetime
import tkinter as tk
import tkcalendar

# --- functions

class MyCalendar(tkcalendar.Calendar):
    def _on_click(self, event):
        print('LABEL:', event.widget)
        print('LABEL text :', event.widget['text'])
        print('LABEL style:', event.widget['style'])
        background = cal.style.configure(event.widget['style'])['background']
        print('LABEL background:', background)
        print('---')
        
        # run original `_on_click`
        super()._on_click(event)

   
# --- main ---

root = tk.Tk()

current_date = datetime.datetime.today()  # PEP8: lower_case_namas

cal = MyCalendar(root, selectmode="day",
                   year=current_date.year, 
                   month=current_date.month,
                   day=current_date.day)
cal.pack(pady=20, padx=20)
cal.bind('<<CalendarSelected>>', on_select)

root.mainloop()

结果(单击日历中的某些日期时):

LABEL: .!mycalendar.!frame2.!label29
LABEL text : 17
LABEL style: normal..!mycalendar.TLabel
LABEL background: white
---
LABEL: .!mycalendar.!frame2.!label32
LABEL text : 20
LABEL style: we..!mycalendar.TLabel
LABEL background: gray80
---

推荐阅读