[Tutor] creating a method name on the fly

Alan Gauld alan.gauld at freenet.co.uk
Mon Aug 7 22:29:15 CEST 2006


>I need to scan a long list of QT tickboxes in a dialog. I need to 
>execute
> pseudo code something like ...
>
> list = ['error_button',  'print_button' ... etc  ]
> for key in list:
> button= list[key]
> print button, self.button.isChecked()
>
>
> where self.button.isChecked() becomes self.error_button.isChecked() 
> ...
> self.print_button.isChecked() ....  etc etc
>
> Can anyone tell me how to do this ? I have looked at apply but with 
> no luck

Why not just add the buttons to a list as you create them then
iterate over the list? Or better still use a dictionary with the name
as key.

buttons['error'] = Button(.....)
buttons['print'] = Button(.....)

then

for name in buttons:
     print name, "button is",
if buttons[name].isChecked(): print "checked"
else: print "unchecked"

No need to mess around trying to dynamically construct buton names 
etc.

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 



More information about the Tutor mailing list