I'm new to Python and wanted to build a sleeptimer for training purposes. I'm using Python 3.6.3 on Ubuntu. I wanted a label which shows how many time is left till the Computer wents to sleep.
I'm using after()
but every time I call the update Method it calls itself very often until I reach the maximum recursion depth with the Error:
RecursionError: maximum recursion depth exceeded while calling a Python object
I think that, for some reason the after(1000, self.update)
command doesn't wait 1 second before calling the method again. Also I don't want a recursion, but call the method each second...
Can you tell me what I'm doing wrong?
class Application(Frame):
def startTimer(self):
self.start = time.time()
self.waiting = self.entry.get()*60000
self.update()
self.after(self.waiting, self.shutdown)
def update(self):
string = str(int(int(self.waiting/6000) + self.start - time.time())/10)
print(string)
self.label.config(text = string + " Minuts left")
self.after(1000, self.update())
def shutdown(self):
print("PSSST!")
def createWidgets(self):
self.entry = Scale(self, from_=5, to=120, orient=HORIZONTAL, resolution=5)
self.label = Label(self,text="time remaining")
self.start = Button(self, text="Start", fg="black",
command=self.startTimer)
self.quit = Button(self, text="QUIT", fg="red",
command=root.quit)
self.entry.pack(side="top")
self.label.pack(side="left")
self.start.pack(side="right")
self.quit.pack(side="bottom")
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack()
self.createWidgets()
root = Tk()
app = Application(master=root)
app.mainloop()`