Python Forum
RuntimeError: threads can only be started once
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
RuntimeError: threads can only be started once
#11
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help To Get Started HansieB 5 920 Nov-29-2024, 03:06 PM
Last Post: HansieB
  Cannot get started standenman 4 2,257 Feb-22-2023, 05:25 PM
Last Post: standenman
  help RuntimeError: no running event loop marpaslight 5 7,736 Oct-18-2022, 10:04 PM
Last Post: marpaslight
  bleak library RuntimeError: This event loop is already running alice93 3 6,382 Sep-30-2021, 08:06 AM
Last Post: alice93
  RuntimeError: generator raised StopIteration quest 1 7,147 Mar-28-2021, 08:11 PM
Last Post: quest
  RuntimeError: This event loop is already running newbie2019 2 7,915 Sep-30-2020, 06:59 PM
Last Post: forest44
  Can't even get started dr6 1 2,115 Aug-18-2020, 04:38 PM
Last Post: Larz60+
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 7,467 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  RuntimeError: can't open file Hadad 2 6,498 Aug-27-2019, 04:23 PM
Last Post: Hadad
  RuntimeError: dictionary changed size during iteration Shreeniket987 3 6,004 Jun-01-2019, 01:22 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020