I was creating a program that lets you enter a date into the GUI and it spits out a label showing the day of the week of said date. Every single time i run the program more than once, it doesn't delete the previous label. If i put in an invalid date and day_of_week
comes back as "INVALID" and then I run it again and get "monday", It would put the monday label in front of "INVALID" instead of deleting "INVALID". How do I delete the previous label after every button click so there is no weird overlap?
from tkinter import *
import datetime
#Declares the gui
root = Tk()
#dimensions of the gui
win_dimensions = '200x100'
def find_day(day,month,year):
try:
#cast year as an integer
year = int(year)
#turn it into a datetime variable
date = datetime.date(year,month,day)
except:
day_of_week = Label(root,text="INVALID DATE",fg="RED")
else:
weekday = date.weekday()
#find the day of week
if weekday == 0:
day_of_week = Label(root,text="Monday")
if weekday == 1:
day_of_week = Label(root,text="Tuesday")
if weekday == 2:
day_of_week = Label(root,text="Wednesday")
if weekday == 3:
day_of_week = Label(root,text="Thursday")
if weekday == 4:
day_of_week = Label(root,text="Friday")
if weekday == 5:
day_of_week = Label(root,text="Saturday")
if weekday == 6:
day_of_week = Label(root,text="Sunday")
finally:
day_of_week.grid(column=1,row=4)
def main():
#title of the pop up window
root.title(" ")
#creates and locks the dimensions of the window
root.geometry(win_dimensions)
root.maxsize(win_dimensions.split("x")[0],win_dimensions.split("x")[1])
root.minsize(win_dimensions.split("x")[0],win_dimensions.split("x")[1])
#label for month
month_label = Label(root,text="Month:")
month_label.grid(column=0,row=1)
#create dropdown menu for month
mon = IntVar()
mon.set(1)
month_num = list(range(1,13))
month = OptionMenu(root,mon,*month_num)
month.grid(column=0,row=2,padx=2)
#label for day
day_label = Label(root,text="Day:")
day_label.grid(column=1,row=1)
#create dropdown menu for day
dy = IntVar()
dy.set(1)
day_num = list(range(1,32))
day = OptionMenu(root,dy,*day_num)
day.grid(column=1,row=2)
#create label for year
year_label = Label(root,text="Year:")
year_label.grid(column=2,row=1)
#create entrybox for year
year = Entry(root,width=5)
year.grid(column=2,row=2,padx=8)
#create button that runs the find_day command
findDay = Button(root,text="Find Weekday",command=lambda:find_day(dy.get(),mon.get(),year.get()),cursor="dot")
findDay.grid(column=1,row=3)
#Calls the window to open
root.mainloop()
#so the program only runs when it is called
if __name__ == "__main__":
main()
CodePudding user response:
You can create day_of_week
label inside main()
, pass it to find_day()
and update its text inside find_day()
:
...
def find_day(day_of_week, day, month, year):
try:
#cast year as an integer
year = int(year)
#turn it into a datetime variable
date = datetime.date(year, month, day)
# get the weekday name using Date.strftime()
day_of_week.config(text=date.strftime('%A'), fg="black")
except:
day_of_week.config(text="INVALID DATE", fg="red")
def main():
...
#create button that runs the find_day command
findDay = Button(root, text="Find Weekday", command=lambda:find_day(day_of_week,dy.get(),mon.get(),year.get()), cursor="dot")
findDay.grid(column=1, row=3)
day_of_week = Label(root)
day_of_week.grid(column=1, row=4)
#Calls the window to open
root.mainloop()
...