ich erstelle während der Laufzeit Buttons, welche ich in eine Array schreibe, um diese danach darüber ansteuern zu können. Das erstellen klappt auch. Dann konfiguriere ich die Buttons durch das Ansteuern der Button, wieder über ein Array. Ich weise Text und Command zu. Beides klappt, jedoch wird beim Command immer der Letzte Wert des Arrays an alle Buttons zugewiesen, obwohl es beim Text jeweils die richtige Zuweisung ist. Kann mir das jemand erklären ?
Ich danke für eure Hilfe schonmal in Vorraus.
Thorsten
Hier ist ein Minimalbeispiel:
Code: Alles auswählen
#!/usr/bin/python
import Tkinter as tk
def show(key):
print key
class NAVIGATION(tk.Tk):
''' An example application for TkInter. Instantiate and call the run method to run. '''
def __init__(self):
tk.Tk.__init__(self)
self.createMenu()
def createMenu(self):
''' Create the Buttons '''
Array_Text = [ "One", "Two","Three" ]
Array_Button = []
# Create Buttons and import in an Array
for key in Array_Text:
Array_Button.append(tk.Button(self, padx=10, pady=5))
# Config the Buttons (Text and Command)
i=0
for key in Array_Text:
Array_Button[i].config(text = key)
Array_Button[i].config(command = lambda: show(key))
Array_Button[i].pack()
i += 1
def run(self):
''' Run the app '''
self.mainloop()
nav = NAVIGATION()
def main():
nav.run()
if __name__ == "__main__":
main()