You're doing a bit more work than you have to. If you bind a callback to the button's click-event explicitly using .bind(...)
, the associated callback takes an event object as a parameter, which has a handle to the widget (button) that triggered the event.
import tkinter as tk
class Application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.title("Buttons")
self.geometry("256x64")
self.resizable(width=False, height=False)
self.buttons = [tk.Button(self, width=4) for _ in range(4)]
for x, button in enumerate(self.buttons):
button.grid(row=0, column=x)
button.bind("<1>", self.on_click)
def on_click(self, event):
event.widget.config(bg="red")
def main():
application = Application()
application.mainloop()
return 0
if __name__ == "__main__":
import sys
sys.exit(main())