Reputation: 5209
problem
I have written a small program to implement a stopwatch. This stopwatch will begin when s
is pressed and stop running when l
is pressed. For this I have used the following code:
f = self.frame
w = self.window
info = Label(f,text="\nPress \'s\' to start running and \'l\' to stop running\n")
info.pack()
w.bind('<KeyPress-s>',self.startrunning)
w.bind('<KeyPress-l>',self.stoprunning)
The stoprunning and start running functions are as so:
def startrunning(self):
r = Frame(self.window)
r.pack()
self.start = time.time()
start = Label(r,text="\nStarted running")
start.pack()
def stoprunning(self):
r = Frame(self.window)
r.pack()
self.stop = time.time()
self.timeConsumed = self.stop - self.start
Label(r,text='\nstopped running').pack()
end = Label(r,text="\nTime consumed is: %0.2f seconds" %self.timeConsumed)
end.pack(side = "bottom")
Error
On pressing the s
key I get the following error:
>>>
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python25\lib\lib-tk\Tkinter.py", line 1414, in __call__
return self.func(*args)
TypeError: startrunning() takes exactly 1 argument (2 given)
Specs Python 2.7
I am new to tkinter programming and am unable to understand what or why this error is being shown. Please tell me if I am using the code correctly. Also please help me resolve this problem.
Upvotes: 0
Views: 203
Reputation: 590
use
def startrunning(self,ev):
def stoprunning(self,ev):
bind send event to a subroutine (http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm).
alternate, you can describe bind as
w.bind('<KeyPress-s>',lambda ev:self.startrunning())
w.bind('<KeyPress-l>',lambda ev:self.stoprunning())
Upvotes: 3