How to get background for cell in tkcalendar?

869 Views Asked by At

I have this calendar and I want to get the background color for selected cell how I can do that?

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

There are 1 best solutions below

0
On

Frankly I don't know why you need to access cells and why you need to know cell's background. If you want to change color in cells then you should rather use tags`

BTW: you don't have to convert date to string and split it

current_date = datetime.datetime.today()

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

tkcalendar doesn't give direct access to cells. But there is hidden cal._calendar which keeps all Labels used to create cells - and this way you could try to access label which you need. But Label doesn't use directly background but style and you would have to convert style to bacground color.


This code display styles for all cells in current month

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

Result:

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
[...]

If you want to access when you click cell then you may have to create own class and overwrite method _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()

Result (when click some dates in calendar):

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