How do I disable all the user input widgets (buttons,entries..) from a parent widget?

前端 未结 1 1142
感动是毒
感动是毒 2021-01-22 09:32

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 widget

相关标签:
1条回答
  • 2021-01-22 10:00

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

    Edit

    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)))
    
    0 讨论(0)
提交回复
热议问题