问题
我写了一个小程序来实现秒表。该秒表将在按下s
时开始,并在按下l
时停止运行。为此,我使用了以下代码:
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)
stoprunning和start running功能如下:
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")
错误
按下s
键后,出现以下错误:
>>>
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)
功能 Python 2.7
我是tkinter编程的新手,我无法理解显示此错误的原因或原因。如果我正确使用代码,请告诉我。另外请帮我解决这个问题。
答案 0 :(得分:3)
使用
def startrunning(self,ev):
def stoprunning(self,ev):
将发送事件绑定到子例程(http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm)。
替代,你可以将bind描述为
w.bind('<KeyPress-s>',lambda ev:self.startrunning())
w.bind('<KeyPress-l>',lambda ev:self.stoprunning())