Edgard Huerta
Edgard Huerta

Reputation: 1

Trouble initiating a DateEntry widget in tkcalendar inside of another class

I want to write a class that creates a paired checkbox and a corresponding DateEntry widget any number of times a new instance is created. All the instances are children to one single window, this class must be able to manipulate the date in the DateEntry widget as follows:

  1. If it's initiated like checkNdate(window, "Fecha de solicitud", ""), then the instance should show the checkbox set in FALSE and NOT SHOW the DateEntry widget

  2. If it's initiated like a particular date like checkNdate(window, "Fecha de solicitud", "16-05-2024"), the checkbox must be shown as TRUE and SHOW the DateEntry widget in the selected date.

  3. If the widget was loaded in the first case, once we hit the checkbox and set it to TRUE, the DateEntry widget must appear and the user should select a date.

  4. If the checkbox is set to FALSE after being in TRUE, the DateEntry widget must be hidden.

  5. The purpose of this is that another button, will connect to a SQL table and send the dates from the DateEntry widgets that have their corresponding checkbox set to TRUE or send None if the checkbox is FALSE.

import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry

class checkNdate(ttk.Frame):
    def __init__(self, parent: ttk.Widget, checkButton_name: str, date_or_none): 
        super().__init__(master=parent)

        # layout of the widgets
        self.rowconfigure(0, weight = 1)
        self.columnconfigure((0 , 1), weight = 1, uniform = "a")
        
        # set the checkbutton
        ttk.Checkbutton(master = self, text = checkButton_name, ).grid(row = 0, column = 0, sticky = "nsew")
        # show the calendar if date exists
        if date_or_none != (None or ""):
            # code to check in which case we are, ignore this print
            print("date is :", date_or_none)
            
            # calling this DateEntry widget creates the error.
            cal = DateEntry(master=parent)
            cal.grid(row = 0, column = 1)
            cal.set_date(date_or_none)
            
            #DateEntry.set_date(date_or_none)
        else:
            # code to check in which case we are, ignore this print
            print("date is :", date_or_none)
            

        self.pack()

# this next code only is for testing purposes
window = tk.Tk()
window.title = "check n' date"
window.geometry("300x300")

# widgets for testing purposes
checkNdate(window, "Fecha de solicitud", "")
checkNdate(window, "Fecha de solicitud", "16-05-2024")

# run
window.mainloop()

I know that so far, the code doesn't bind the checkbox to the creation or destruction of the DateEntry widget but my current problem is that this error pops when I try to load the DateEntry on those two examples. I think is a class inheritance issue but I'm not sure

date is : 
date is : 16-05-2024
Traceback (most recent call last):
  File "/Users/hidari/Coding/cs50py/project/prueba_class_check_calendar.py", line 42, in <module>
    checkNdate(window, "Fecha de solicitud", "16-05-2024")
  File "/Users/hidari/Coding/cs50py/project/prueba_class_check_calendar.py", line 24, in __init__
    cal = DateEntry(master=parent)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/tkcalendar/dateentry.py", line 123, in __init__
    self._top_cal = tk.Toplevel(self)
                    ^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/tkinter/__init__.py", line 2695, in __init__
    self.title(root.title())
               ^^^^^^^^^^^^
TypeError: 'str' object is not callable

Upvotes: 0

Views: 53

Answers (0)

Related Questions