I want to get which button was clicked within for loop but i can't able to get button inside the function and the button is generated by for loop and code is this:
from tkinter import *
from tkinter import ttk
class Application(Tk):
def __init__(self):
super(Application, self).__init__()
self.create_grid()
def create_grid(self):
for i in range(1, 4):
self.btn= ttk.Button(self, text="", command=lambda btn=self.btn: self.clik(btn))
self.btn.grid(ipadx=20, ipady=20, row=i, column=0)
def clik(self, btn):
print(btn)
if __name__ == '__main__':
app = Application()
app.mainloop()
Error:
self.btn= ttk.Button(self, text="", command=lambda btn=self.btn: self.clik(btn))
File "fakepath\__init__.py", line 2346, in __getattr__
return getattr(self.tk, attr)
AttributeError: '_tkinter.tkapp' object has no attribute 'btn'
Thanks For Any Help.
self.btn.configure(command=lambda btn=self.btn: self.clik(btn))from functools import partialand thenself.btn.config(command=partial(self.click, self.btn))(obviously place the import at the beginning of the code)