Destroying widgets from a different subroutine in tkinter Destroying widgets from a different subroutine in tkinter tkinter tkinter

Destroying widgets from a different subroutine in tkinter


First, place() returns None so SClabelspare==None not a Tkinter ID. Second it is local, so is garbage collected when the function exits. You have to keep a reference to the object which can be done in many ways. A Python tutorial would be a good idea to get the basics before you go further https://wiki.python.org/moin/BeginnersGuide/Programmers Also, programming a Tkinter app without using class structures is a frustrating experience, unless it is something very simple. Otherwise you get errors like yours and have to spend much time and effort trying to overcome them. This is an example that I already have and is meant to to give a general idea of the process.

from Tkinter import *from functools import partialclass ButtonsTest:   def __init__(self):      self.top = Tk()      self.top.title("Click a button to remove")      Label(self.top, text="Click a button to remove it",            bg="lightyellow").grid(row=0)      self.top_frame = Frame(self.top, width =400, height=400)      self.button_dic = {}      self.buttons()      self.top_frame.grid(row=1, column=0)      Button(self.top_frame, text='Exit', bg="orange",             command=self.top.quit).grid(row=10,column=0, columnspan=5)      self.top.mainloop()   ##-------------------------------------------------------------------            def buttons(self):      b_row=1      b_col=0      for but_num in range(1, 11):         ## create a button and send the button's number to         ## self.cb_handler when the button is pressed         b = Button(self.top_frame, text = str(but_num),                     command=partial(self.cb_handler, but_num))         b.grid(row=b_row, column=b_col)         ## dictionary key=button number --> button instance         self.button_dic[but_num] = b         b_col += 1         if b_col > 4:            b_col = 0            b_row += 1   ##----------------------------------------------------------------   def cb_handler( self, cb_number ):      print "\ncb_handler", cb_number      self.button_dic[cb_number].grid_forget()##===================================================================BT=ButtonsTest()


Or, if this is supposed to be very simple, without a lot of hard-to-manage global variables, and if class structures would only introduce needless complexity, you might try something like this (it worked for me in the python3 interpreter from the command line):

from tkinter import *root = Tk()def victim():    global vic    vic = Toplevel(root)    vicblab = Label(vic, text='Please bump me off')    vicblab.grid()def bumper():    global vic    bump = Toplevel(root)    bumpbutt = Button(bump, text='Bump off', command=vic.destroy)    bumpbutt.grid()victim()bumper()