I am trying to create a timer in Tkinter. This is my working example:
# GUI packages
from Tkinter import *
from ttk import *
from tkMessageBox import *
sec = 0
#_
class RunMeasurement(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
Tk.style = Style()
Tk.title = 'Data sheet creation'
self.__timeLabel = None
myFrame = Frame(self)
myFrame.pack(fill='both')
self.__timeLabel = Label(myFrame)
timeButton = Button(myFrame, text='start', command=self.Tick)
timeButton.grid(row=0, column=0)
self.__timeLabel.grid(row=0, column=1)
def Tick(self):
global sec
sec += 1
self.__timeLabel['text'] = sec
self.__timeLabel.after(1000, self.Tick)
if __name__ == '__main__':
app = RunMeasurement()
app.mainloop()
But if I try to change it a little bit in order to, for example, count each time by 2 or 3 in this way (only corresponding part of the previous code was changed):
...
timeButton = Button(myFrame, text='start', command=lambda: self.Tick(3))
...
def Tick(self, index):
...
sec += index
...
self.__timeFrame.after(1000, self.Tick(index))
I get this (see the picture) very fast. And then the RuntimeError:
RuntimeError: maximum recursion depth exceeded while calling a Python object
Question: how to handle this error? And how to create such kind of timer in Tkinter?