Time Changing Without Clicking Button

2024/11/16 3:44:56

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.

Answer

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()
https://en.xdnf.cn/q/120373.html

Related Q&A

Avoid `logger=logging.getLogger(__name__)` without loosing way to filter logs

I am lazy and want to avoid this line in every python file which uses logging:logger = logging.getLogger(__name__)In january I asked how this could be done, and found an answer: Avoid `logger=logging.g…

Find all directories and files of your laptop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

How to format and write excel using pandas

Lets assume that we have nested python dictionaries which should be written in single excel sheet file. Following are sample dictionaries which can be used. Car = [{"carbmodel": "Model A…

Openpyxl is unable to read after modifying

Requirement : 1.create a gui using Tkinter 2.Update the excel by fetching values from Tkinter entry widget 3.Read another sheet of the same workbook 4.plot graph using inside the Tkinter window.Prob…

Rounding datetime based on time of day

I have a pandas dataframe with timestamps shown below:6/30/2019 3:45:00 PMI would like to round the date based on time. Anything before 6AM will be counted as the day before. 6/30/2019 5:45:00 AM -&g…

Scraping Project Euler site with scrapy [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python date function bugs

I am trying to create a function in python which will display the date. So I can see the program run, I have set one day to five seconds, so every five seconds it will become the next day and it will p…

Retreiving data from a website [duplicate]

This question already has answers here:How to determine the IP address of the server after connecting with urllib2?(4 answers)Closed 9 years ago.Im terribly sorry if this is unacceptable or answered e…

How to comma separate an array of integers in python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 5 years ago.Improve…

Python 2.7.5 - Where is it installed on Windows Vista? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about a specific programming problem, a software algorithm, or s…