Johnnyyy
Johnnyyy

Reputation: 103

How to destroy a specific widget at once ? Tkinter

When i pressed the "Create New" button a submit button will be appear at the bottom. But whenever i choose the drop down value the submit button will be destroyed and a new button called "Update" will be shown. But the problem is that, when I repeatedly choose the drop down value and go back to "Create New" button, the "update" button can only be destroy once. Is it a way to find all the update button and destroy all at once ? Thanks

When i pressed create new button :

enter image description here

When i choose a few times of drop down list value and go back to "create new", the submit and update button is stacked together :

enter image description here

Code:

def submit():
    deleteAllEntry()
    global CreateJsonBtn, profilenameLbl, ProfileNameEntry
    CreateJsonBtn = Button(jsonframe, text="Submit", command=submit, width=11)
    CreateJsonBtn.place(x=370, y=540)
    CreateNewJsonBtn.configure(state=DISABLED)

    UpdateJsonBtn.destroy()

#Get value inside the dropdown value
def dropdown(choice):
    if (jsonprofileName == ""):
        tkMessageBox.showinfo("ERROR", "Fail to retrieve value. Try again later", icon="error")
    else:
        cursor = con.cursor()

        cursor.execute("select * from json where jsonprofilename='" +options.get() + "'")

        rows = cursor.fetchall()

        deleteAllEntry()
    global row
    for row in rows:
        insertAllEntry()

    cursor.close()

    try:
    #Delete submit button when select existing value
        CreateJsonBtn.destroy()
        CreateNewJsonBtn.configure(state=NORMAL)
    except:
        pass

    finally:
        global UpdateJsonBtn
        UpdateJsonBtn = Button(jsonframe, text="Update", command=submit, width=11)
        UpdateJsonBtn.place(x=370, y=550)





Upvotes: 0

Views: 528

Answers (2)

astqx
astqx

Reputation: 2096

Instead of destroying and creating buttons multiple times, you can configure them, check the example below

from tkinter import *

def foo():
    print('called foo')
    button.configure(text='bar',command=bar)

def bar():
    print('called bar')
    button.configure(text='foo',command=foo)

root=Tk()

button=Button(root,text='foo',command=foo)
button.pack()

root.mainloop()

config/configure is a universal widget method, you can use this refernece to learn more about them.

The reason you are currently facing the issue is that every time the dropdown is used, a new button "Update" is placed on top of the previous one (if one already exists). The UpdateJsonBtn holds the last assigned instance and hence deletes the topmost one, if there are buttons underlying, they will not be destroyed. I would not suggest the approach currently used by you.

In the question you stated if there is a way to get all the buttons that say "Update", you can do that using the following (again, I am not suggesting to use this in your case, but just to answer your question)

for widget in jsonframe.winfo_children():
    if isinstance(widget,Button):
        if widget['text']=='Update':
            widget.destroy()

Upvotes: 1

FileX
FileX

Reputation: 791

So I know that it should work with CreateJsonBtn.place_forget()

You can try that

Upvotes: 0

Related Questions