Disabling tkinter ttk scale widget
我正试图禁用一个框架中的所有(TTK)小部件,但似乎缩放小部件给我带来了一些麻烦,因为它引发了以下异常:
_tkinter.TclError: unknown option"-state"
号
一些相关代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import tkinter as tk from tkinter import ttk def disable_widgets(parent): for child in parent.winfo_children(): child.config(state = 'disabled') root = tk.Tk() # Frame full of widgets to toggle frame_of_widgets = ttk.Frame(root) frame_of_widgets.pack() # Button to be disabled button_to_disable = ttk.Button(frame_of_widgets) button_to_disable.pack() # Entry to be disabled entry_to_disable = ttk.Entry(frame_of_widgets) entry_to_disable.pack() # Scale to be disabled scale_to_disable = ttk.Scale(frame_of_widgets) scale_to_disable.pack() # Button that disables widgets in frame disable_button = ttk.Button(root,text="Disable",command= lambda: disable_widgets(frame_of_widgets)) disable_button.pack() root.mainloop() |
它适用于按钮和输入,但不适用于刻度。我认为TTK的一个好处是使小部件与常用的方法和属性更加一致,所以我猜可能我访问这三个小部件都不正确?
提前感谢您的帮助!
对于TTK小部件,使用
您可以这样重写函数:
1 2 3 | def disable_widgets(parent): for child in parent.winfo_children(): child.state(["disabled"]) |
这里的ttk文档中提到了ttk状态(尽管描述边界是无用的):https://docs.python.org/3.1/library/tkinter.ttk.html小部件状态