how to stop tkinter timer function when i press button one more times?

2024/10/5 22:31:08

i'd tried to use root.after_cancel(AFTER), but i dont know how.

    root.after_cancel(AFTER) 
AFTER = None
def countdown(count,time,name):global AFTERtime['text'] =name,":",datetime.fromtimestamp(count).strftime("%M:%S")if count > 0 :AFTER = root.after(1000, countdown, count-1,time,name)elif count == 0 :time['text'] = f"Возрожден:{name}"``` 
Answer

It's hard to guess what actually you are after, but ... below working code which outputs to stdout and is able to start/stop/restart the countdown with pressing the [Countdown] button. The 'trick' is to run the timer all the time checking a value of a global variable, where pressing the button sets the value of that variable depending on what value it currently has. The way the code works appears to be an answer to both of your questions (1. how to stop and 2. how to reset the countdown by pressing the same button with which the countdown was started).

def countdown():global countdownValueif countdownValue > 0 :print(' countdown: ', countdownValue)countdownValue -= 1if countdownValue == 0 :countdownValue = -1print(f"\n  Возрожден:  {name} :) ")root.after(1000, countdown)def startStopCountdown():global countdownValueif countdownValue > 0:print('stopCountdown')countdownValue = -1else: print('startCountdown')countdownValue = 10import tkinter as tk
countdownValue = -1
name           = 'Возро'root = tk.Tk()
root.title("3,2,1,...")
btn = tk.Button( root, text="Countdown", font=('',20), command=startStopCountdown)
btn.grid()
countdown()
root.mainloop()

By the way: in the context of your question following comes to my mind as maybe helpful in the context of your question Add a label/text (ideally positioned above a countdown timer) using tkinter .

https://en.xdnf.cn/q/119733.html

Related Q&A

Read csv into database SQLite3 ODO Python

I am trying to read in a csv into a new table in a new databased using ODO, SQLite3 and Python.I am following these guides:https://media.readthedocs.org/pdf/odo/latest/odo.pdf http://odo.pydata.org/en/…

netmiko cant execute sh run | i host

I notice that my netmiko code cant run sh run | i host which is a legitimate Cisco command.When I replace sh run with other command such as sh clo, or show ip interface brief, it works perfectly.from n…

How to dump the data from file to an excel sheet

I want to dump [3-4 lines together] some data to an excel sheet. I could able to dump single line based on some criteria [like if line is getting start with // or /* ], but in case of when lines starts…

I dont understand why my script is not iterating through all string.split elements?

The objective of this python exercise is to build a function that turns text into pig latin, a simple text transformation that modifies each word by moving the first character to the end and appending …

Unable to change the tick frequency on my chart

I have seen many questions on changing the tick frequency on SO, and that did help when I am building a line chart, but I have been struggling when its a bar chart. So below are my codes import numpy a…

Django Queryset foreign keys

I am trying to get a queryset but it is not displaying anything. Basically, I want to get the Asset objects that are assigned via foreign key to an employee, which is a foreign key of the signed in use…

How to reorder the columns of a CSV?

How can I re-order the columns of a CSV file using Python? These are the first rows of a CSV file I need to change:03;30269714;Ramiro Alberto;Nederz;active;pgc_gral 03;36185520;Andrea;Espare;active;pg…

Distance matrix in Python Pandas

I am a newbie in python, but I like to process data in pandas. I have a hundred pairs of CSV data such as passenger and bus stop data. The passenger structure data is Person, and XY coordinates (UTM-Me…

calculating catalan numbers using memoization

I am tring to use memoization in order to calculate catalan numbers, but it just does not seem to work, what do I need to change?def catalan_mem(n, memo = None):if n==0:return 1if memo == None:memo = …

cron python file doesnt work on centos 7

im trying to schedule my pythohn script into Centos 7 with cron. On my script at start i have added this:#!/usr/local/bin/pythonand this is my cron file that i have create into folder that contain pyth…