The following code works fine as long as the time in the spinbox does not change. What I want is to do the set the time for a break. The first time it works perfectly but after that if I change the value in the spinbox, the system will check for new value instead of the value which was in the spinbox when the button is clicked.
from tkinter import *
from tkinter import ttk
from datetime import datetimeroot = Tk()
root.title("Fusion Calculator")first_break_time_label = Label(root, text='1st Break:')
first_break_time_label.grid(row=0, column=0)first_break_time_hour = Spinbox(root, from_=0, to=23, format='%02.0f')
first_break_time_hour.grid(row=0, column=1)first_break_time_minute = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_minute.grid(row=0, column=2)first_break_time_second = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_second.grid(row=0, column=3)loop_active = False # global variable set to falsedef check_breaks(): # new loop function that runs set_breaks and reschedulesset_breaks() # for 1 second later.root.after(1000, check_breaks)def set_breaks():check_break = datetime.now().strftime("%H:%M:%S")first_break_time_value = f'{first_break_time_hour.get()}:{first_break_time_minute.get()}:{first_break_time_second.get()}'if check_break == first_break_time_value:popup_first_break = Toplevel(root)popup_first_break.title('Official Break')popup_first_break_title_bar = Label(popup_first_break, text='OFFICIAL BREAK', fg='Red')popup_first_break_title_bar.pack()else:passglobal loop_activeif not loop_active: # This will only run when loop_active is falseloop_active = True # Set loop_active to True so it doesn't run againcheck_breaks()set_breaks_button = ttk.Button(root, text='Set', command=set_breaks)
set_breaks_button.grid(row=1, column=3)root.mainloop()
I tried to bind the button with a mouse click so that it will create an array and get the values into the array. But I am having hard time working with events.
The quick and easy way to do it, would be to create a dictionary that holds each of the breaks values initially set to None, then to populate the dictionary the first time the button is pressed and start the loop at the same time, then have the loop check the dictionary on each iteration for the break times, and set the button command to a function that updates the dictionary when pressed.
For example:
from tkinter import *
from tkinter import ttk
from datetime import datetimeroot = Tk()
root.title("Fusion Calculator")first_break_time_label = Label(root, text='1st Break:')
first_break_time_label.grid(row=0, column=0)first_break_time_hour = Spinbox(root, from_=0, to=23, format='%02.0f')
first_break_time_hour.grid(row=0, column=1)first_break_time_minute = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_minute.grid(row=0, column=2)first_break_time_second = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_second.grid(row=0, column=3)loop_active = False # global variable set to falsebreaks = {1: None
}def check_breaks(): # new loop function that runs set_breaks and reschedulesset_breaks() # for 1 second later.root.after(1000, check_breaks)def update_break():first_break_time_value = f'{first_break_time_hour.get()}:{first_break_time_minute.get()}:{first_break_time_second.get()}'breaks[1] = first_break_time_valueset_breaks()def set_breaks():check_break = datetime.now().strftime("%H:%M:%S")if check_break == breaks[1]:popup_first_break = Toplevel(root)popup_first_break.title('Official Break')popup_first_break_title_bar = Label(popup_first_break, text='OFFICIAL BREAK', fg='Red')popup_first_break_title_bar.pack()global loop_activeif not loop_active: # This will only run when loop_active is falseloop_active = True # Set loop_active to True so it doesn't run againcheck_breaks()set_breaks_button = ttk.Button(root, text='Set', command=update_break)
set_breaks_button.grid(row=1, column=3)root.mainloop()
What might be a better option though would be to start the timed loop at the start of the program that way you can get rid of the loop active variable and extra check function.
Like this:
from tkinter import *
from tkinter import ttk
from datetime import datetimeroot = Tk()
root.title("Fusion Calculator")first_break_time_label = Label(root, text='1st Break:')
first_break_time_label.grid(row=0, column=0)first_break_time_hour = Spinbox(root, from_=0, to=23, format='%02.0f')
first_break_time_hour.grid(row=0, column=1)first_break_time_minute = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_minute.grid(row=0, column=2)first_break_time_second = Spinbox(root, from_=0, to=59, format='%02.0f')
first_break_time_second.grid(row=0, column=3)breaks = {1: None
}def update_break():first_break_time_value = f'{first_break_time_hour.get()}:{first_break_time_minute.get()}:{first_break_time_second.get()}'breaks[1] = first_break_time_valuedef check_breaks():check_break = datetime.now().strftime("%H:%M:%S")if check_break == breaks[1]:popup_first_break = Toplevel(root)popup_first_break.title('Official Break')popup_first_break_title_bar = Label(popup_first_break, text='OFFICIAL BREAK', fg='Red')popup_first_break_title_bar.pack()root.after(1000, check_breaks)check_breaks()
set_breaks_button = ttk.Button(root, text='Set', command=update_break)
set_breaks_button.grid(row=1, column=3)root.mainloop()