farlescharris
farlescharris

Reputation: 11

How do I create a button that clears off all labels in tkinter?

What my code does currently is create a label with the text "Hello" every time I press the button that says "Say hello"

What I'm trying to figure out is how to create a button that clears off all of the labels off the screen, however I'm completely clueless.

How do I create a button that clears all of the labels off of the screen?

My code is down below.

import tkinter as tk
import time

root = tk.Tk()
root.geometry("700x500")

h = "Hello"

def CreateLabel():
    helloLabel = tk.Label(root, text=h)
    helloLabel.pack()

labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()

root.mainloop()

Upvotes: 1

Views: 68

Answers (1)

titoo
titoo

Reputation: 55

I'm beginner but try this:

import tkinter as tk
import time

root = tk.Tk()
root.geometry("700x500")

h = "Hello"
liste = []

def CreateLabel():
    helloLabel = tk.Label(root, text=h)
    helloLabel.pack()
    liste.append(helloLabel)

def DelLabel():
     for i in range(len(liste)):
        liste[i].destroy()
     liste.clear()


labelButton = tk.Button(root, text="Say hello", command=CreateLabel)
labelButton.pack()
labelButton = tk.Button(root, text="del hello", command=DelLabel)
labelButton.pack()

root.mainloop()

Upvotes: 2

Related Questions