Creating Button arrays with different commands using Tkinter

Harlin harlinseritt at yahoo.com
Tue Feb 22 08:04:24 EST 2005


I have an array of Appnames. Let's say they are 'Monkeys', 'Cats',
'Birds'.

I would like create a button array with these:

---Start Code---
# Constructing and displaying buttons
for a in Appnames:
   Button(root, text=a, command=lambda:self.OpenFile(a)).pack()

# Somewhere else I have this function defined within the class...
def OpenFile(self, AppName):
   fh = open(AppName+".txt", 'r').read()
   do something with fh

---End Code---

When I do this, whatever the last value a was is what the each button's
command option is. Does anyone know what I can do to differentiate each
button so that it has its own separate argument for self.OpenFile?

Thanks,

Harlin




More information about the Python-list mailing list