Home > Enterprise >  Destroy tkinter button
Destroy tkinter button

Time:10-13

I created an application in Tkinter Python that searches an archive for several licenses and then groups the results in worldlists. After finding a license, a button appears which, if I press it, shows me the result in a ScrolledText. The idea is that I cannot delete the buttons that appear (to restart) after I have made a search. Can someone help me to solve this situation. How can I destroy the mit_bt?


def print_to_textbox_Mit(wordlist):
    if len(wordlist) >= 1:
        mit_button()

def mit_button():
    mit_bt = tk.Button(window, text= " M I T ", relief="groove", command=mit_print_text, activebackground="#800020").place(x=10, y=10)

def mit_print_text():
    text_box.delete("1.0", tk.END)
    text_box.insert("0.0", "\n            !!!!!! MIT !!!!!!")
    for lines in wordlist:
        text_box.insert("end", "\n" lines)

CodePudding user response:

def print_to_textbox_Mit(wordlist):
    if len(wordlist) >= 1:
        mit_button()

def mit_button():
    mit_bt = tk.Button(
        window,
        text= " M I T ",
        relief="groove",
        activebackground="#800020",
    )
    # configure your button's 'command' after declaring it above
    mit_bt.configure(command=lambda b = mit_bt: mit_print_text(b))
    # using a lambda here allows us to pass an argument to the 'command' callback!
    mit_bt.place(x=10, y=10)

def mit_print_text(button):  # pass your button as an argument to this function
    text_box.delete("1.0", tk.END)
    text_box.insert("0.0", "\n            !!!!!! MIT !!!!!!")
    for lines in wordlist:
        text_box.insert("end", "\n" lines)
    # once you no longer need the button, call 'place_forget' on it to remove it
    button.place_forget()
    

CodePudding user response:

If you want the button to go away and your mit_print_text function results to be the only thing displaying, you can add "mit_bt.destroy()" at the end of that function. As JRiggles commented, you would have to pass it in.

def mit_print_text(mt_bt):
    text_box.delete("1.0", tk.END)
    text_box.insert("0.0", "\n            !!!!!! MIT !!!!!!")
    for lines in wordlist:
        text_box.insert("end", "\n" lines)
    mit_bt.destroy()
  • Related