温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Disabling tkinter ttk scale widget
python tkinter ttk user-interface

python - 禁用tkinter ttk比例小部件

发布于 2020-04-04 00:03:25

我正在尝试禁用框架中的所有(ttk)小部件,但似乎小部件比例尺给我带来了一些麻烦,因为它引发了以下异常:

_tkinter.TclError:未知选项“-状态”

一些相关的代码:

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

查看更多

提问者
Msg
被浏览
28
Bryan Oakley 2015-06-09 23:40

对于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