I'm trying to make the timer slow down in a function or even stop the timer when a function is called.
Tried doing .remove .forget but nothing would work. Any ways to stop/slow down the timer?
from tkinter import *
import time
class App():
def __init__(self):
self.window = Tk()
self.root = Frame(self.window, height=200,width=200)
self.root.pack()
self.root.pack_propagate(0)
self.window.title('Timer')
self.label = Label(text="")
self.label.pack()
self.sec = 11
self.timerupdate()
self.root.mainloop()
def timerupdate(self):
if self.sec!=0:
self.sec-=1
self.label.configure(text=self.sec)
self.root.after(1000, self.timerupdate)
if self.sec == 0:
self.sec = 11
self.slow_time()
def slow_time(self):
self.after.configure(1000000000,self.counting)
app=App()
app.mainloop()
The only timer in the code is when you call self.root.after(1000, self.timerupdate)
. The 1000
is what controls the speed. It designates how far in the future to call a function, and is represented as a number of milliseconds. If you want to run something every second, use the value 1000
. If you want to run something every two seconds, the value is 2000
, and every half second would be 500
.
So, store the speed in a variable, use that variable when you call self.after
, and then you simply change that variable whenever you want to speed up or slow down the timer.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.