如何从父小部件禁用所有用户输入小部件(按钮、条目...)? [英] How do I disable all the user input widgets (buttons,entries..) from a parent widget?

查看:37
本文介绍了如何从父小部件禁用所有用户输入小部件(按钮、条目...)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 和 Tkinter 设计 GUI.注册用户输入命令所需的所有按钮和条目都放置在主 frame 中,并且是它们的子小部件.

I am designing a GUI using Python and Tkinter. All the buttons and entries required to register the user input commands are placed inside a main frame and are their child widgets.

我想知道是否可以通过将一些禁用"标志从主框架传播到所有输入小部件来禁用这些小部件的所有输入功能.通过这种方式,我希望能够在一行代码中切换它们的状态.

I want to know if it is possible to disable all the input functionality from these widgets by propagating some "disable" flag from the main frame to all the input widgets. In this way, I was hoping to be able to toggle their state in a single line of code.

我相信这应该是可能的.有人知道怎么做吗?

I believe that should be possible. Does anyone know how to do this?

推荐答案

Tk 小部件有一个 state 配置选项,可以是 normaldisabledem>.因此,您可以使用框架上的 winfo_children 方法将框架的所有子项设置为禁用以遍历它们.例如:

Tk widgets have a state configuration option that can be either normal or disabled. So you can set all children of a frame to disabled using the winfo_children method on the frame to iterate over them. For instance:

for w in app.winfo_children():
    w.configure(state="disabled")

Ttk 小部件具有可能需要其他处理的状态方法.您可能还想将 takefocus 选项设置为 False,尽管我认为移动焦点时会自动跳过禁用的小部件(例如:通过按 Tab 键).

Ttk widgets have state method which might require alternative handling. You may also want to set the takefocus option to False as well although I think that disabled widgets are automatically skipped when moving the focus (eg: by hitting the Tab key).

编辑

如果需要访问包含在子框架中的小部件,您可以使用 winfo_childrenwinfo_parent 方法在两个方向上遍历小部件树.例如,访问根小部件的每个子部件的简单函数:

You can use the winfo_children and winfo_parent methods to walk the widget tree in both directions if necessary to access widgets contained in child frames for instance. For example, a simple function to visit each child of a root widget:

def visit_widgets(root, visitor):
  visitor(root)
  for child in root.winfo_children():
    visit_widgets(child, visitor)

from __future__ import print_function
visit_widgets(app, lambda w: print(str(w)))

这篇关于如何从父小部件禁用所有用户输入小部件(按钮、条目...)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆