Dynamically Creating Functions and Binding Buttons in Tkinter
The problem is that you are using the last value of self.button_id
when you call the command once the buttons are created. You have to bind the current value of the local variable for each lambda with lambda i=i: do_something_with(i)
:
def Create_Widgets(self): for i in range(1, 11): self.newmessage = Button(self, text= "Button ID: %d" % i, anchor=W, command = lambda i=i: self.access(i)) self.newmessage.config(height = 3, width = 100) self.newmessage.grid(column = 0, row = i, sticky = NW)