Question: How do I delete a button when it is clicked if it isn't assigned to a variable ?
Core point:
.bind('<Button-1>', self.on_click)
Using a event callback, provides you with the Grid
coordinate to replace the removed Button
with a other widget, like a Label
with a image.
import tkinter as tk
class Button(tk.Button):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
self.bind('<Button-1>', self.on_click)
def on_click(self, event):
w = event.widget
row, column = w.grid_info().get('row'), w.grid_info().get('column')
print('coord:{}'.format((row, column)))
w.destroy()
class App(tk.Tk):
def __init__(self):
super().__init__()
for row in range(0, 9):
for column in range(0, 9):
Button(self, text=" ").grid(row=row, column=column)
if __name__ == '__main__':
App().mainloop()