Home > Net >  How to delete Label before printing a new one
How to delete Label before printing a new one

Time:10-30

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

CodePudding user response:

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()
  • Related