-1
  from tkinter import *


class Main:

    def __init__(self, root):

        for i in range(0, 9):
            for k in range(0, 9):
                Button(root, text=" ").grid(row=i, column=k)


        root.mainloop()


root = Tk()

x = Main(root)

How do I delete a button when it is clicked if it isn't assigned to a variable ?

Reza Rahemtola
  • 1,182
  • 7
  • 16
  • 30
  • 1
    Do you need to delete _all_ buttons? If not, how will you decide whether you need to delete a certain button? – Ed Ward Feb 12 '20 at 19:46
  • check this question, it might help https://stackoverflow.com/questions/47883821/check-what-button-in-a-list-has-been-pressed-with-bind-tkinter – Henrik Feb 12 '20 at 19:49
  • 2
    There are a lot of questions on this site related to working with buttons created in a loop. Have you done any research before asking? This is a very common problem. – Bryan Oakley Feb 12 '20 at 19:53
  • Does this answer your question? [tkinter destroy button after click](https://stackoverflow.com/questions/37840196/tkinter-destroy-button-after-click) – linamnt Feb 12 '20 at 21:17

2 Answers2

2

lambda is your friend.

When dealing with loops you have 2 major options. Both options use a lambda to maintain the values per button in a loop like this.

One is to have the button destroy itself:

import tkinter as tk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        for i in range(10):
            for k in range(10):
                btn = tk.Button(self, text='  ')
                btn.config(command=lambda b=btn: b.destroy())
                btn.grid(row=i, column=k)


if __name__ == '__main__':
    App().mainloop()

Or use a counter and a list. I prefer this list method as we can do a lot of things with a list like this if we need to.

import tkinter as tk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.btn_list = []
        counter = 0
        for i in range(10):
            for k in range(10):
                self.btn_list.append(tk.Button(self, text='  '))
                self.btn_list[-1].config(command=lambda c=counter: self.destroy_btn(c))
                self.btn_list[-1].grid(row=i, column=k)
                counter += 1

    def destroy_btn(self, ndex):
        self.btn_list[ndex].destroy()


if __name__ == '__main__':
    App().mainloop()
Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
1

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()

stovfl
  • 14,998
  • 7
  • 24
  • 51