禁用后如何(重新)启用 tkinter ttk Scale 小部件?

How to (re)enable tkinter ttk Scale widget after it has been disabled?

我试图在 Python tkinter 中禁用它后重新启用我的 Scale 小部件,但它不起作用。我尝试了多种选择,但 none 有效。

s.state(["normal"]);
s.configure(state='normal');

我得到的错误是:

_tkinter.TclError: unknown option "-state"

由于您使用 ttk 小部件,因此您需要重新启用小部件的状态是 !disabled

根据ttk states

A state specification or stateSpec is a list of state names, optionally prefixed with an exclamation point (!) indicating that the bit is off.

try:
    import tkinter as tk
    import tkinter.ttk as ttk
except ImportError:
    import Tkinter as tk
    import ttk


root = tk.Tk()

scale = ttk.Scale(root)
scale.pack()

#   disable scale
scale.state(['disabled'])
#   enable scale
scale.state(['!disabled'])

root.mainloop()