All-on, all-off button for checkbuttons?

Hans Nowak wurmy at earthlink.net
Mon Feb 25 15:06:19 EST 2002


Nick Arnett wrote:
> 
> I'm just bringing myself up to speed with Python, so forgive what I think is
> an simple question.  I'm building a UI that has a bunch of checkbuttons,
> which also includes buttons to turn all the checkbuttons on or off.  I see
> how to turn them off individually, but it seems to me that there must be a
> more elegant way.
> 
> 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.

Without knowing what GUI (e.g. Tkinter, wxPython, Qt, etc) you are using,
I can only give you an example in pseudo-Python...

In __init__, keep a list of checkbox instances around:

  c1 = CheckBox(...)
  c2 = CheckBox(...)
  c3 = CheckBox(...)
  self.checkboxes = [c1, c2, c3]

Then, when you want to do something with all these
checkboxes, simply loop over the list:

  for checkbox in self.checkboxes:
      checkbox.checked = false

Again, this is pseudo-Python, the names and method in your 
program will look different. But you'll get the idea.

Depending on the GUI you're using, there should probably
be a way to find all objects on a form/frame, and loop over
them. That would be another way to do it.

HTH,

-- 
Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==')) 
# decode for email address ;-)
The Pythonic Quarter:: http://www.awaretek.com/nowak/



More information about the Python-list mailing list