dynamic buttons with tkinter

Markus von Ehr markus.vonehr at ipm.fhg.de
Mon Jul 1 07:41:16 EDT 2002


Dear Mirko,

use a lambda function as command for the button and pass a name or
number to that function,
this is a way to identify the clicked button:

  for file in os.listdir( dir ):
        tmp_func_load = lambda index=0, obj=self: obj.load(file)
        Button( load_dlg_frame, text=file, relief=GROOVE,
                command=tmp_func_load).pack()

def  load(self, filename):
    pass

I pack the whole stuff into a class, that's the reason for the selfs,
but
it works without it, too.

Markus



Mirko Koenig schrieb:

> Hi
>
> In my i want to make a button for every file
> in a special directory, so that i can load it.
> I did:
>
>  for file in os.listdir( dir ):
>         Button( load_dlg_frame, text=file, relief=GROOVE,
>                 command=load).pack()
>
> But how can i find out which button is pressed?
> Or another question: How can i pass a event funktion more than 1
> parameter (the event and other things).
> I can't give any button a new event function, because it's dynamic,
> because of the number of files in the directory.
>
> thanx
>
> Mirko




More information about the Python-list mailing list