Search code examples
python-3.xtkinterdestroy

Python tkinter destroying a label


I know there have been many sources that have shown how to use the destroy command but for some reason this ignores the destroy command and continues to create more text labels. Any ideas on why this is? The code is for a small game im making.

You can find the labels i want to destroy under "integrity()"

    from tkinter import *
from random import randrange
class Window(Frame):
    def position(self):
        return {"x":randrange(0,350),"y":randrange(0,250)}
    def __init__(self,master=None):
        Frame.__init__(self,master)
        self.master = master
        self.__init__window()
    def __init__window(self):
        global count
        count=0
        self.master.title("GUI")
        self.pack(fill=BOTH, expand=1)
        self.Button1 = Button(self, text="Click me",command=self.Message)
        self.Button1.place(**self.position())
        self.Button2 = Button(self, text="Click me if you can",command=self.integrity)
        self.Button2.place(**self.position())
        menu=Menu(self.master)
        self.master.config(menu=menu)
        file = Menu(menu)
        file.add_command(label="Exit", command=self.client_exit)
        menu.add_cascade(label="File",menu=file)
        edit = Menu(menu)
        edit.add_command(label="Starto", command=self.showText)
        menu.add_cascade(label="Edit", menu=edit)
    def Message(self):
        print("Hello world")
        self.Button1.place(**self.position())
    def showText(self):
        text = Label(self, text="Clicks: ")
        text.pack()
    def integrity(self):
        self.Button2.place(**self.position())
        global count
        count=count+1
        self.text1 = Label(self, text=count)
        self.text1.destroy()
        self.text1 = Label(self,text=count)
        self.text1.pack()
        print("Clicks: ",count)
        if count<5:
            print("(づòДó)づ")
            print("Dont click!")
            print("﴾´• ω •`﴿\n")
        elif count<10:
            print("ヽ(òДó)ノ")
            print("Stop it")
            print("(づòДó)づ\n")
        elif count<15:
            print("﴾⇀∀↼﴿")
            print("Stop it please!\n")
        else:
            print("Fine you win just stop! ლ(>Д<ლ)\n")
        def client_exit(self):
        exit()
root = Tk()
root.geometry("400x300")
app = Window(root)
root.mainloop()

Solution

  • The problem is these three lines of code:

    self.text1 = Label(self, text=count)
    self.text1.destroy()
    self.text1 = Label(self,text=count)
    

    You are creating a label, immediately destroy it, and then creating it again. So, at the end of this code you have a label.

    The next time the code is called, you already have one label. Then you create another label (so, two), then immediately destroy it (so, one), and then immediately create another (so, two).

    The next time the code is called, you already have two labels. Then you create another label (so, three), then immediately destroy it (so, two), and then immediately create another one (so, three)

    ... and so on.

    The solution is to remove those three lines of code, create self.text1 inside __init__window, and then just change the text on the label each time the function is called.