简体   繁体   English

禁用 tkinter ttk 缩放小部件

[英]Disabling tkinter ttk scale widget

I am trying to disable all of the (ttk) widgets in a frame, but it appears that the scale widget is giving me some trouble, as it throws the following exception:我正在尝试禁用框架中的所有 (ttk) 小部件,但似乎缩放小部件给我带来了一些麻烦,因为它引发了以下异常:

_tkinter.TclError: unknown option "-state" _tkinter.TclError:未知选项“-state”

Some relevant code:一些相关代码:

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()

It works for the button and entry, but not for the scale.它适用于按钮和条目,但不适用于比例。 I thought one of the benefits of ttk was making widgets more uniform with common methods and attributes, so I am guessing perhaps I am accessing all three of these widgets incorrectly?我认为 ttk 的好处之一是使用通用方法和属性使小部件更加统一,所以我猜也许我错误地访问了所有这三个小部件?

For ttk widgets you use the state method.对于 ttk 小部件,您使用state方法。 The state method for buttons and entry widgets are just a convenience function to mimic the standard button and entry widgets.按钮和条目小部件的state方法只是模仿标准按钮和条目小部件的便利功能。

You can rewrite your function like this:你可以像这样重写你的函数:

def disable_widgets(parent):
    for child in parent.winfo_children():
        child.state(["disabled"])

ttk states are mentioned in the ttk documentation here (though the description borders on useless): https://docs.python.org/3.1/library/tkinter.ttk.html#widget-states这里的 ttk 文档中提到了 ttk 状态(尽管描述几乎无用): https ://docs.python.org/3.1/library/tkinter.ttk.html#widget-states

another way:其它的办法:

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

You can consider that set the breakpoint at the configure of the class Scale ( from tkinter.ttk import Scale ) may get some helpful.您可以考虑在 Scale 类的configure处设置断点( from tkinter.ttk import Scale )可能会有所帮助。

The following is part of the code to intercept the class Scale下面是拦截class Scale部分代码

class Scale(Widget, tkinter.Scale):

    ...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM