amsi_25
amsi_25

Reputation: 11

I'm receiving an attribute error when trying to disable a cell in tkCalendar using coordinates

My code to disable the date cells on tkCalendar is as follows:

from tkcalendar import Calendar
from datetime import datetime
import tkinter as tk
from tkinter import *

def dates_play(self,date_entry):
        #Creating the calendar display
        self.cal_display = Calendar(self, selectmode = "day",date_pattern="mm-dd-yyyy", disableddaybackground="red")

        #Placing the widget in a way that it fills up the cell W+E+N+S
        self.cal_display.grid(row=1, rowspan=8,column=1, columnspan=2, sticky=tk.W+E+N+S, padx=10, pady=10)
        
        #Converts the date into a date_time object
        date_entry =datetime.strptime(date_entry, "%m-%d-%Y")

        # Retruning the day of the week as a number(X)
        day_no=date_entry.weekday()

        #Returing the week of the month as a number(Y)
        month_no = date_entry.isocalendar()[1]- date_entry.replace(day=1).isocalendar()[1]

        #Disabling that date
        self.cal_display = self.cal_display._calendar[month_no][day_no].state(['disabled'])

Error:

`Traceback (most recent call last):
  File "c:\Users\Lenovo\OneDrive\Documents\Amm_Uni\BOE Leave Management App\BOE_Leave_Management.py", line 339, in <module>
    app = LeaveBookingApp()
  File "c:\Users\Lenovo\OneDrive\Documents\Amm_Uni\BOE Leave Management App\BOE_Leave_Management.py", line 29, in __init__
    self.create_widgets()
  File "c:\Users\Lenovo\OneDrive\Documents\Amm_Uni\BOE Leave Management App\BOE_Leave_Management.py", line 65, in create_widgets
    self.confirm_button = tk.Button(self, text="Confirm", bg="#a6db9c", width=21, height=2, font= 'Times 15', command = self.dates_play("08-25-2023"))
  File "c:\Users\Lenovo\OneDrive\Documents\Amm_Uni\BOE Leave Management App\BOE_Leave_Management.py", line 320, in dates_play
    self.cal_display = self.cal_display[day_no][month_no].state(['dsiabled'])
  File "C:\Users\Lenovo\AppData\Local\Programs\Python\Python310\lib\site-packages\tkcalendar\calendar_.py", line 514, in __getitem__
    raise AttributeError("Calendar object has no attribute %s." % key)
AttributeError: Calendar object has no attribute 4.`

I want the dates to be disabled i.e, they will turn grey and the user cannot select them.

Upvotes: 0

Views: 73

Answers (1)

amsi_25
amsi_25

Reputation: 11

As self._calendar is the internal list that holds all the dates, I had to add in ._calendar:

self.cal_display._calendar[day_no][month_no]

Upvotes: 0

Related Questions