pythontkintertkcalendar

tkcalendar throws AttributeError on get_date() method


I want to get the selected date of a tkcalendar calendar but it throws an AttributeError: 'NoneType' object has no attribute 'get_date' and I don't know why.

Can somebody help me?

Here's my code:

class Main(Frame):
    def __init__(self, parent):
        super(Main, self).__init__(parent)

        padding = 10

        # calenders
        mainCalendarFrame = Frame(root)


        # start
        startCalendarFrame = Frame(mainCalendarFrame)

        startLabel = Label(startCalendarFrame, text='Start').pack(fill=X, pady=(0, padding))
        self.startCalendar = Calendar(startCalendarFrame, selectmode='day', year=int(date.today().strftime('%Y')), month=int(date.today().strftime('%m')), day=int(date.today().strftime('%d'))).pack(fill=X, pady=(0, 5))


        mainCalendarFrame.pack(padx=padding, pady=padding)
        settingsFrame.pack(fill=X, padx=padding, pady=padding)
        btnCalculate.pack(fill=X, padx=padding, pady=padding)

    def btn_click(self):
        print(self.startCalendar.get_date())

Solution

  • That's why it is not recommended to use the .pack(), .grid(), .place() methods immediately when defining a label because it returns None You should split the command, and instead of what you did, proceed like this:

    self.startCalendar = Calendar(...)
    self.startCalendar.pack(...)
    

    I had the same problem before and it was really annoying.