python-3.xtkintertkcalendar

Tkcalendar get_date gets passed to a function but returns TypeError


Im trying to get a date from tkcalaendar and pass it to a function where it can be saved as a class varible. It works the first time when the button is pressed however the second time it returns "TypeError: 'str' object is not callable"

from tkinter import *
from tkcalendar import *
import datetime

class test_class():
    
    selected_date = ""
    
    def __init__(self):
        self.window = Tk()
        
        self.stu_cal = Calendar(self.window,selectmode="day",year=int(test_class.get_year()),month=int(test_class.get_month()))
        self.stu_cal.grid(row=9,column=0)
        
        self.b3 = Button(self.window,text="Select this date",bg='#B6BDC4',fg='white',command=self.add_selected_date)
        self.b3.grid(row=9,column=6)
        
        self.window.mainloop()
        
    def add_selected_date(self):
        test_class.selected_date(self.stu_cal.get_date())
        
    @staticmethod    
    def get_year():
        currentDateTime = datetime.datetime.now()
        date = currentDateTime.date()
        return date.strftime("%Y")
    
    @staticmethod 
    def get_month():
        currentDateTime = datetime.datetime.now()
        date = currentDateTime.date()
        return date.strftime("%m")
    
    @classmethod
    def selected_date(cls,cal_date):
        cls.selected_date = cal_date

test_class()

Solution

  • You have used same name selected_date for both class variable and class method.

    Suggest to rename the class method to set_selected_date():

    class test_class():
    
        selected_date = ""
    
        def __init__(self):
            self.window = Tk()
    
            self.stu_cal = Calendar(self.window,selectmode="day",year=int(test_class.get_year()),month=int(test_class.get_month()))
            self.stu_cal.grid(row=9,column=0)
    
            self.b3 = Button(self.window,text="Select this date",bg='#B6BDC4',fg='white',command=self.add_selected_date)
            self.b3.grid(row=9,column=6)
    
            self.window.mainloop()
    
        def add_selected_date(self):
            # use new function name
            test_class.set_selected_date(self.stu_cal.get_date())
    
        @staticmethod
        def get_year():
            currentDateTime = datetime.datetime.now()
            date = currentDateTime.date()
            return date.strftime("%Y")
    
        @staticmethod
        def get_month():
            currentDateTime = datetime.datetime.now()
            date = currentDateTime.date()
            return date.strftime("%m")
    
        # renamed to set_selected_date
        @classmethod
        def set_selected_date(cls,cal_date):
            cls.selected_date = cal_date