0

I want to delete a button I made in python tkinter and I am taking the button name as input. After getting the input I want to find the button with the same name and delete it

from tkinter import *
root= Tk()

class nodeCreate:
    def __init__(self, ip, name):
        global Name 
        self.ip= ip
        self.name=name 
        Name = Button(device_show, text= name, bg= "red", command= lambda: view(name))
        Name.pack()
        
    def delete_node(Name):
        print(f"I will be deleating {Name}")
        pass

class nodeManage:
    def new():
        top= Toplevel()
        text= Label(top, text="Enter the Data")
        text.grid(row=1, column=1)# starting row 1 column 1 
        namedes= Label(top, text="Enter the name")
        namedes.grid(row=2, column=1)
        name= Entry(top)
        name.grid(row=2, column=2)
        ipdes= Label(top, text="Enter the ip")
        ipdes.grid(row=3, column=1)# same row column 1
        ip= Entry(top)
        ip.grid(row=3, column=2 ) #same row column  2 
        ok= Button(top, text="ok", command= lambda: nodeCreate(ip.get(), name.get()))
        ok.grid(row=4, column=3)
    def delete_node():
        top= Toplevel()
        info= Label(top, text="Enter the button name")
        info.grid(row=1, column=1)
        name= Entry(top)
        name.grid(row=1, column=2)
        ok= Button(top, text="ok", command= lambda: nodeCreate.delete_node(name.get()))
        ok.grid(row=3, column=2)

    def view(name):
        print(name)

device_show= LabelFrame(root, text="select", padx= 50, pady= 50)
device_show.pack()
devices_label= Label(device_show, text="Your nodes")
devices_label.pack()

welcom= Label(root, text="Welcom")
welcom.pack()
add= Button(root, text="Add client", command= nodeManage.new)
add.pack()
delete_device= Button(root, text="delete", command= nodeManage.delete_node)
delete_device.pack()

My first try was to change the class of the string object to button object but it did't work

Wolf
  • 9,679
  • 7
  • 62
  • 108
  • 1
    I tried to fix the indentation in the code shown in the box, BTW: your methods look wrong to me, are they really intended to not take a `self` parameter? – Wolf May 12 '21 at 07:18
  • Is maybe the question [*How to delete Tkinter widgets from a window?*](https://stackoverflow.com/questions/12364981/how-to-delete-tkinter-widgets-from-a-window) ***and its answers*** helpful to figure out what to do? It seems to reduce the task to finding the widget. – Wolf May 12 '21 at 07:20
  • well `delete_node()` does absolutely no deleting stuff and just prints some text out and I have to agree `nodeManage` class looks pretty "horrible" as already stated – Matiiss May 12 '21 at 08:19
  • Another hint gives [*Add custom attributes to a Tk widget*](https://stackoverflow.com/q/42329556/2932052), you are talking about *names* of *buttons*; maybe it's easier to add such a feature to widgets. – Wolf May 12 '21 at 10:06

1 Answers1

0

I came up with two options to mount a custom attribute name to buttons, one is subclassing as shown in Add custom attributes to a Tk widget and one is using the built-in function setattr. My remove_widget implementation iterates all children of root removing all having a name attribute matching the given value:

from tkinter import *

root = Tk()

def remove_widget(name):
    for w in root.slaves():
        if getattr(w, 'name', '') == name:
           w.pack_forget()

# Option #1 subclassing:

class NButton(Button):
    def __init__(self, master, name=None, *args, **kwargs):
        Button.__init__(self, master, *args, **kwargs)
        self.name = name
b1 = NButton(root, text="Delete me #1", name='b1', command=lambda: remove_widget("b1"))
b1.pack()

# Option #2 Python's setattr function:

b2 = Button(root, text="Delete me #2", command=lambda: remove_widget("b2"))
setattr(b2, 'name', 'b2')
b2.pack()

root.mainloop()

Since b2 is already an object, we can just add the name attribute with the easier dot syntax instead:

b2 = Button(root, text="Delete me #2", command=lambda: remove_widget("b2"))
b2.name = 'b2'
b2.pack()
Wolf
  • 9,679
  • 7
  • 62
  • 108