How to delete Label before printing a new one

Question:

My python code has a ui calendar and button but i don’t know how to get the label to print and after destroying the label before it

from tkinter import*
from tkcalendar import*

root=Tk()
root.title("Code project")

def selectDate():
    myDate =my_Cal.get_date()
    selectedDate = Label(text=myDate)
    selectedDate.pack()



my_Cal= Calendar(root, setmode = 'day', date_pattern = 'd/m/yy')
my_Cal.pack()

openCal = Button(root, text="Select Date", command=selectDate)
openCal.pack()

root.mainloop()

This keeps reprinting the new selected date once the button is clicked under the old selected date. You can see what it does in the image below.

you may need to zoom in a bit

Tis is what happens after i click the button

Asked By: L bozo

||

Answers:

You have to define your label just once and update just it’s text like this:

from tkinter import*
from tkcalendar import*

root=Tk()
root.title("Code project")

selectedDate = Label(root, text="")

def selectDate():
    myDate =my_Cal.get_date()
    selectedDate.config(text=myDate)
    selectedDate.pack()



my_Cal= Calendar(root, setmode = 'day', date_pattern = 'd/m/yy')
my_Cal.pack()

openCal = Button(root, text="Select Date", command=selectDate)
openCal.pack()

root.mainloop()
Answered By: Rodrigo Guzman
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.