Tkinter Countdown, doesn't cancel previous countdown when starting a new

24 views Asked by At

I have created a script that will check for and download/install windows updates. Then give the user the possibility to postpone restart up to 3 times. Each time user presses postpone i want my current countdown to be set to 0 or be cancelled, so only 1 countdown is visible. Right now when i postpone it sets the current countdown to 60, but the previous countdown comes up in between. For example, i have 7 minutes left, i press postpone, countdown says 60 minutes, after maybe 40 seconds it says 6 minutes left and 20 seconds later it updates to 59. I have tried using ChatGPT to give me an alternative but it doesn't come up with anything usefull

It's this function def cancel_shutdown(): i can't get to work

def proceed_to_countdown():
    update_label.config(text="Forbereder genstart.")
    schedule_restart(10)

def schedule_restart(minutes):
    global countdown_job
    cancel_countdown() # Cancel any previous countdowns
    run_command(f"shutdown -r -t {minutes * 60}")
    countdown_job = update_countdown(minutes)

def cancel_countdown():
    global countdown_job
    if countdown_job is not None:
        root.after_cancel(countdown_job)
        countdown_job = None

def update_countdown(minutes):
    global countdown_job
    if minutes > 0:
        update_label.config(text=f"Computeren genstarter om {minutes} minutter.")
        countdown_job = root.after(60000, lambda: update_countdown(minutes - 1))
    else:
        update_label.config(text="Genstarter nu...")

def abort_shutdown():
    try:
        subprocess.run("shutdown /a", check=True, shell=True)
        update_label.config(text="Genstart udsat.")
        return True
    except subprocess.CalledProcessError as e:
        print(f"An error occurred: {e}")
        return False

I tried using Copilot and ChatGPT to generate alternatives, but it doesn't give any results. I am axpecting a countdown my users can rely on.

0

There are 0 answers