Being a student in the times of the coronavirus, schools are not open and therefore I must work at home, while still following a specific timetable. I have made a virtual planner using tkinter in python that tells me what lesson I have, how long left I have (using a progress bar) and what lesson I have next. This is what it looks like.
But when the lesson changes, the old lesson still remains, and the new one is just written over it. It then looks like this. RE (Religious education) is written above maths.
The code I have is this:
from tkinter import *
from datetime import datetime, date
from tkinter.ttk import Progressbar
from tkinter import ttk
import math
num = 0
window = Tk()
window.title("Planner")
monday = ['Maths', 'RE', 'Break', 'English', 'Spanish', 'Lunch', 'Music']
tuesday = ['Science', 'History', 'Break', 'Technology', 'Maths', 'Lunch', 'Geography']
wednesday = ['English', 'Games', 'Break', 'Drama', 'Computing', 'Lunch', 'Science']
thursday = ['Art', 'Geography', 'Break', 'Spanish', 'Computing', 'Lunch', 'English']
friday = ['PE', 'History', 'Break', 'Maths', 'Science', 'Lunch', 'English']
timing = [845, 945, 1045, 1100, 1200, 1300, 1350, 1450]
def update():
global num
global day
global time
now = datetime.now()
hour = now.strftime("%H")
minu = now.strftime("%M")
day = now.strftime("%A")
time = (hour+minu)
if int(time) < 1449 and int(time) > 845:
for i in timing:
if int(time) < int(timing[num]):
lesson_time = (timing[num-1])
break
num = num + 1
num = num - 1
if day == 'Monday':
text = StringVar()
label = Label(window, textvariable = text, font=("Arial Bold", 10), fg='darkblue')
label.grid(row=1, column=0)
text.set(monday[num])
if num <= 5:
next_up = Label(window, text = ("Next: " + monday[num+1]), font=("Arial Bold", 10), fg='darkblue')
next_up.grid(row=1, column=1)
elif day == 'Tuesday':
text = StringVar()
label = Label(window, textvariable = text, font=("Arial Bold", 10), fg='darkblue')
label.grid(row=1, column=0)
text.set(tuesday[num])
if num <= 5:
next_up = Label(window, text = (" Next: " + tuesday[num+1] + ' '), font=("Arial Bold", 10), fg='darkblue')
next_up.grid(row=1, column=1)
elif day == 'Wednesday':
text = StringVar()
label = Label(window, textvariable = text, font=("Arial Bold", 10), fg='darkblue')
label.grid(row=1, column=0)
text.set(wednesday[num])
if num <= 5:
next_up = Label(window, text = ("Next: " + wednesday[num+1]), font=("Arial Bold", 10), fg='darkblue')
next_up.grid(row=1, column=1)
elif day == 'Thursday':
text = StringVar()
label = Label(window, textvariable = text, font=("Arial Bold", 10), fg='darkblue')
label.grid(row=1, column=0)
text.set(thursday[num])
if num <= 5:
next_up = Label(window, text = ("Next: " + thursday[num+1]), font=("Arial Bold", 10), fg='darkblue')
next_up.grid(row=1, column=1)
elif day == 'Friday':
text = StringVar()
label = Label(window, textvariable = text, font=("Arial Bold", 10), fg='darkblue')
label.grid(row=1, column=0)
text.set(friday[num])
if num <= 5:
next_up = Label(window, text = ("Next: " + friday[num+1]), font=("Arial Bold", 10), fg='darkblue')
next_up.grid(row=1, column=1)
else:
no_school = Label(window, text = 'Relax - No School!', font=("Arial Bold", 10), fg='darkblue')
no_school.grid(row=0, column=0)
if day != 'Saturday' or 'Sunday':
style = ttk.Style()
style.theme_use('default')
style.configure("black.Horizontal.TProgressbar", background='black')
bar = Progressbar(window, length=100, style='black.Horizontal.TProgressbar', max=60)
if num == 2:
target = 15
elif num == 5:
target = 50
else:
target = 60
diviser = 60/target
difference = str(timing[num])[-2:]
top = (int(timing[num+1])) - int(difference)
bottom = (int(timing[num])) - int(difference)
round_time = int(minu) - int(difference)
if top < 0:
top = top + 60
if round_time < 0:
round_time = round_time + 60
bar['value'] = (round_time*diviser)
bar.grid(column=1, row=0)
else:
if day == 'Saturday' or day == 'Sunday':
no_school = Label(window, text = 'Relax - No school!', font=("Arial Bold", 10), fg='darkblue')
no_school .grid(row = 0, column = 0)
else:
no_school= Label(window, text = 'Relax - No School!', font=("Arial Bold", 10), fg='darkblue')
no_school.grid(row = 0, column = 0)
update()
if not day == 'Saturday' or 'Sunday':
if not (int(time) > 1449) and not (int(time) < 844):
update = Button(window, text='Update', command=update)
update.grid(row=0, column = 3)
window.mainloop()
I tried label.destroy()
before changing the label text (changing the subject). This did not produce an error message but just did not solve the problem.
Also, I have another problem. It is that I have to manually press 'update' so that it tells me the up-to-date information. Is there a way to have it update automatically? I tried putting update()
in a for loop, but this did not work.