All-on, all-off button for checkbuttons?

Fredrik Lundh fredrik at pythonware.com
Tue Feb 26 17:22:11 EST 2002


Nick Arnett wrote:
> The checkbuttons are created in __init__.  I'm thinking that there must be a
> way to filter or iterate through the objects in __init__, and for each one,
> if it's a button, select or deselect as appropriate.  I see how to get the
> namespace as a dictionary, but that hasn't helped.

a better approach is to loop over the children of the
master widget, and use isinstance.

something like this could work:

from Tkinter import *

root = Tk()

for x in range(10):
    var = IntVar()
    w = Checkbutton(root, text=str(x), variable=var)
    w.pack()
    w.var = var # keep a reference

def set_all(value):
    for w in root.children.values():
        if isinstance(w, Checkbutton):
            w.var.set(value)

w = Button(root, text="on", command=lambda: set_all(1))
w.pack()

w = Button(root, text="off", command=lambda: set_all(0))
w.pack()

mainloop()

</F>

<!-- (the eff-bot guide to) the python standard library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list