Reputation: 3
How do I make the destroy button destroy itself with the destroy() function, and still be able to make a new destroy button with the create button?
from tkinter import Button, Tk
def create():
b2 = Button(root, text='Destroy', command=destroy)
b2.pack()
def destroy():
b2.destroy()
root = Tk()
b1 = Button(root, text='Create', command=create)
b1.pack()
root.mainloop()
Upvotes: 0
Views: 290
Reputation: 2331
Try this:
from tkinter import *
def create():
b = Button(root, text='Destroy')
b.config(command=destroy(b))
b.pack()
def destroy(button):
def inner():
button.destroy()
return inner
root = Tk()
b1 = Button(root, text='Create', command=create)
b1.pack()
root.mainloop()
We create a different callback for each new button, so we don't have to worry about scopes, and we can have more than destroy-able button.
Upvotes: 1