DeMeNToR
DeMeNToR

Reputation: 13

I cant destroy buttons tkinter

i have this code:

from tkinter import *

root = Tk()
root = root
root.title("1")
root.geometry('600x650')

def click():
   b.destroy()
b = (1, 2, 3, 4, 5)
for i in b:
   b = Button(root, text=i, command=click)
   b.pack()

print("success")

root.mainloop()

i need destroy button, which i clicked, but it destroy the last generated button.

pls, do not advise code like this:

from tkinter import *

root = Tk()
root = root
root.title("1")
root.geometry('600x650')

def click1():
    button1.destroy()
def click2():
    button2.destroy()

button1 = Button(root, text="1", command=click1)
button2 = Button(root, text="2", command=click2)
button1.pack()
button2.pack()

print("success")

root.mainloop()

I need each button to be able to close itself.

Upvotes: 0

Views: 528

Answers (3)

Niraj
Niraj

Reputation: 51

You can try this one

from tkinter import *

root = Tk()
root = root
root.title("1")
root.geometry('600x650')
bts = [1, 2, 3, 4, 5]

f = Frame(root)
f.pack()

def click(val):
    global bts, root, b, f

    f.destroy()
    f = Frame(root)
    f.pack()

    bts.remove(val)
    for i in bts:
        b = Button(f, text=i, command=lambda x=i: click(x))
        b.grid(row=int(i - 1), column=1)


for i in bts:
   b = Button(f, text=i, command=lambda x=i: click(x))
   b.grid(row=int(i-1), column=0)

print("success")

root.mainloop()

Upvotes: 0

tgikal
tgikal

Reputation: 1680

In this section you are defining the tuple as b, then overwriting b in each iteration:

b = (1, 2, 3, 4, 5)
for i in b:
   b = Button(root, text=i, command=click)
   b.pack()

But the main problem is there is only ever 1 button in b, what you want is each button to be stored in a list such as:

from tkinter import *

root = Tk()
root = root
root.title("1")
root.geometry('600x650')

def click(idx):
    # Here x is used to determine the button's location in list b.
    b[idx].destroy()

buttons = (1, 2, 3, 4, 5)
b = [] # Make b a list.
for x, i in enumerate(buttons):
    b.append(Button(root, text=i,
                    # Here lambda stores x for each iteration, and assigns it to click.
                    command= lambda idx = x: click(idx)))
    b[-1].pack() # Pack the last button appended to b.

print("success")

root.mainloop()

Basically the same thing, using lambda, but I already typed it so I'm posting it.

Upvotes: 0

Axe319
Axe319

Reputation: 4365

Here is an example of what I meant in my comment.

from tkinter import *
from functools import partial

root = Tk()
root = root
root.title("1")
root.geometry('600x650')

# specify which button to click
def click(index):
    buttons[index].destroy()

b = (1, 2, 3, 4, 5)
# keeping our buttons in a list allows us to destroy them individually
buttons = []
for index, i in enumerate(b):
    # use functools.partial to hand arguments to the callback function
    # the argument being the button you are clicking  
    buttons.append(Button(root, text=i, command=partial(click, index)))
    buttons[index].pack()

print("success")

root.mainloop()

Here is your example you gave and where it is failing.

from tkinter import *

root = Tk()
root = root
root.title("1")
root.geometry('600x650')

# the function destroys b
# that b is the last button you created in your loop below
def click():
   b.destroy()

# you create a tuple of numbers and assign it to b
b = (1, 2, 3, 4, 5)
for i in b:
   # for every loop a button is created and packed and assigned to b
   # but that is overwritten on every subsequent loop 
   # since you keep assigning to b
   b = Button(root, text=i, command=click)
   b.pack()

print("success")

root.mainloop()

Upvotes: 1

Related Questions