Nov-24-2024, 03:24 PM
(Nov-24-2024, 02:48 PM)Raysz Wrote:(Nov-24-2024, 12:45 PM)deanhystad Wrote: Like this:
import tkinter as tk import schedule import time import threading def job(): print("I'm working...") # this is just for testing return schedule.CancelJob # remove it from the scheduler def time_up(): schedule.clear() print('time has elapsed') # this is just for testing def schedule_time(): """Add a job to the scheduler""" schedule.every().day.at(start_time.get()).do(job) schedule.every().day.at(stop_time.get()).do(time_up) def schedule_thread(): """Run pending jobs.""" while running: # For debugging, print list of pending jobs. time.sleep(10) print(schedule.get_jobs()) schedule.run_pending() def end_program(): """End schedule thread so program can exit.""" global running running = False root.destroy() root = tk.Tk() root.protocol("WM_DELETE_WINDOW", end_program) start_time = tk.StringVar() tk.Label(root, text='Start (HH:MM[:SS]):').grid(row=0, column=0, padx=10, pady=10) tk.Entry(root, width=8, textvariable=start_time).grid(row=0, column=1, padx=10) stop_time = tk.StringVar() tk.Label(root, text='Stop (HH:MM[:SS]):').grid(row=1, column=0, padx=10, pady=10) tk.Entry(root, width=8, textvariable=stop_time).grid(row=1, column=1, padx=10) tk.Button( root, text="Run", command=schedule_time ).grid(row=2, column=0, columnspan=2, padx=10, pady=10, sticky="news") running = True scheduler_thread = threading.Thread(target=schedule_thread) scheduler_thread.start() root.mainloop()At the start of the program create a thread that runs the schedule. This thread runs for the entirety of the program. The run button adds a new job to the schedule. Scheduling a job does not start a new thread. When shutting down the program you need to stop the scheduler thread.
Thank you very much for your help it does work the way it's supposed to I appreciate it
I will look it over and compare with what I have and see where I made the error
it's a great learning tool thank you very much
So I do see the difference and I think my problem was threading I didn't quite understand how it worked
I am curious though I was always taught to put my window root = tk.Tk() at the top of my script I'm curious to know why
you put yours on the bottom is that just preference