Vimo Vimo - 3 months ago 8
Python Question

Button Widget Not Getting Destroyed While Trying To Remove From The Grid

a. Have a scenario where I wanted to remove a button after few clicks.
b. But when the button reaches the last click, its not getting destroyed.
Code as given below:

class test_button:
def __init__(self, master):
self.master = master
self.next_button = None

if not (self.next_button):
self.next_button = Button(root, background="orange red", activebackground="orangered3", text="Next Test Config", command=self.next_button_code).grid(row=1, column=1)

def next_button_code(self):
if self.next_button:
self.next_button.destroy(); self.next_button = None

# Top Local Variables
root = Tk()

# Top Level Default Codes
my_gui = test_button(root)

root.mainloop()


Am I missing anything ? Kindly drop in your comments !!

Answer

Change

self.next_button = Button(root, background="orange red", activebackground="orangered3", text="Next Test Config", command=self.next_button_code).grid(row=1, column=1)

to:

self.next_button = Button(root, background="orange red", activebackground="orangered3", text="Next Test Config", command=self.next_button_code)
self.next_button.grid(row=1, column=1)