禁用tkinter ttk scale小部件

时间:2015-06-09 15:17:54

标签: python user-interface tkinter ttk

我正在尝试禁用框架中的所有(ttk)小部件,但看起来缩放小部件给我带来了一些麻烦,因为它会引发以下异常:

  

_tkinter.TclError:未知选项" -state"

一些相关代码:

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的一个好处是使用常见的方法和属性使小部件更加统一,所以我猜我可能错误地访问了所有这三个小部件?

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:4)

对于ttk小部件,您使用state方法。按钮和条目小部件的state方法只是模拟标准按钮和条目小部件的便利功能。

您可以像这样重写您的功能:

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#widget-states

答案 1 :(得分:0)

另一种方式:

scale_to_disable.configure(state='disabled')  # 'normal'

您可以考虑在Scale类(configure)的from tkinter.ttk import Scale处设置断点可能会有所帮助。

以下是拦截class Scale

的代码的一部分
class Scale(Widget, tkinter.Scale):

    ...

    def configure(self, cnf=None, **kw):
        if cnf:
            kw.update(cnf)
        Widget.configure(self, **kw)