Home > Net >  PYTHON - How to change data entry state to normal?
PYTHON - How to change data entry state to normal?

Time:12-17

I can disable the calendar widget with the 'Disable' button, but if I want to reactivate it, I get the following error:

tkinter.TclError: unknown option "-normal"

If i try to use the calendar.config(state="normal") command, I get the following error:

tkinter.TclError: unknown option "-state"
import tkinter as tk 
import ttkbootstrap as ttk

def disable_calendar():
    calend["state"]="disabled"

def enable_calendar():
    calend["state"]="normal"


root = ttk.Window() 
root.geometry("400x400")

calend =  ttk.DateEntry(bootstyle="primary") 
calend.place(x=10, y=10)     

disableCalendar_button = ttk.Button(text = "Disable", width=10, command=disable_calendar) 
disableCalendar_button.place(x=10, y=50)

enableCalendar_button = ttk.Button(text = "Enable", width=10, command=enable_calendar) 
enableCalendar_button.place(x=100, y=50)

root.mainloop()

CodePudding user response:

Looking at the source code for the ttkbootstrap DateEntry widget shows that it inherits from the ttk.Frame class, which itself doesn't accept state changes. However, it looks like the following states are allowed because DateEntry overrides the built-in configure method:

  • 'disabled'
  • 'readonly'
  • 'invalid'

As far as I can tell, the default "normal" state is 'readonly', but I'm not sure.

class DateEntry(ttk.Frame)
...
    def _configure_set(self, **kwargs):
        """Override configure method to allow for setting custom
        DateEntry parameters"""

        if "state" in kwargs:
            state = kwargs.pop("state")
            if state in ["readonly", "invalid"]:
                self.entry.configure(state=state)
            elif state == "disabled":
                self.entry.configure(state=state)
                self.button.configure(state=state)
            else:
                kwargs[state] = state
  • Related