0
votes

I had to use an Entry widget in the 'readonly' state, then I wanted to change the text with the insert method : to do so, I understand that it is needed to put the widget in the state 'normal', but I get an error : is that "normal"?

For instance,

    entry = ttk.Entry(root,width=30)
    entry.insert(0,"Please enter your name")
    entry.state(['readonly'])
    entry.state(['normal'])

Gives me as a result :

File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/ttk.py", line 595, in state return self.tk.splitlist(str(self.tk.call(self._w, "state", statespec))) _tkinter.TclError: Invalid state name normal

1

1 Answers

1
votes

Confusingly, ttk widgets have two different concepts of "state". One is a configuration option named state, and one is a method named state. The configuration option is a shorthand to change the editability of the widget. The method is used to get and set several internal states used for styling.

These two ways of specifying state accept different values. The "readonly" state is one state that is valid on both, but "normal" is only valid for the configuration option.

To use the state method, you pass in a stateSpec which is a list of one or more states or negated states. For example, to switch between readonly and not readonly you would do it this way:

entry.state(["readonly"])
entry.state(["!readonly"])

This manner of specifying a widget state is much more flexible and fine-grained, and is designed to allow complex styling. For example, a widget can at one time be in the states readonly, disabled, invalid, and hover.

The simpler way to configure whether the widget is editable or not is with the state configuration option. It must always be one of the string values "normal", "disabled", or "readonly".

entry.configure(state="readonly")
entry.configure(state="normal")

The allowable values for the configuration option are fairly self-explanatory. The values for building up a stateSpec via the state method are the following:

  • active - The mouse cursor is over the widget and pressing a mouse button will cause some action to occur. (aka “prelight” (Gnome), “hot” (Windows), “hover”).
  • disabled - Widget is disabled under program control (aka “unavailable”, “inactive”).
  • focus - Widget has keyboard focus.
  • pressed - Widget is being pressed (aka “armed” in Motif).
  • selected - “On”, “true”, or “current” for things like checkbuttons and radiobuttons.
  • background - Windows and the Mac have a notion of an “active” or foreground window. The background state is set for widgets in a background window, and cleared for those in the foreground window.
  • readonly - Widget should not allow user modification.
  • alternate - A widget-specific alternate display format. For example, used for checkbuttons and radiobuttons in the “tristate” or “mixed” state, and for buttons with -default active.
  • invalid - The widget's value is invalid. (Potential uses: scale widget value out of bounds, entry widget value failed validation.)
  • hover - The mouse cursor is within the widget. This is similar to the active state; it is used in some themes for widgets that provide distinct visual feedback for the active widget in addition to the active element within the widget.