[Tkinter-discuss] creating widgets generically, with a loop?

Martin Franklin mfranklin1 at gatwick.westerngeco.slb.com
Thu Jun 22 09:15:08 CEST 2006


Mike Steiner wrote:
> Does this work - does the "command=" parameter allow the function
> value to contain parameters too? If so, it would solve the problem
> easily!
> 
> --- Martin Franklin <mfranklin1 at gatwick.westerngeco.slb.com> wrote:
>> [ stuff omitted ]
>> for text, external, filename in my_buttons:
>>    b = Button(self, text=text, command=self.launchit(external,
>> filename))
>>    b.grid()
> 


Mike,


Sorry I should have provided some doc strings ;)  The magic in my
example is here:-

## this function is called when you create the Button widget
def launchit(external, filename):
    external_short = os.path.split()[1]

    ## this inner function has access to the outer functions 	
    ## namespace
    def real_launchit():
        os.spawnl(os.P_NOWAIT, external, external_short, filename)

    ## return a reference to the inner function
    ## this 'reference to a function' will be called when the button
    ## is pressed
    return real_launchit


on re-reading the example I noticed I had made a couple of typoes

Cheers
Martin





More information about the Tkinter-discuss mailing list