0

问题

我写了一个小程序来实现秒表。此秒表将在按下时开始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 编程新手,无法理解显示此错误的内容或原因。请告诉我我是否正确使用了代码。另外请帮我解决这个问题。

4

1 回答 1

3

采用

def startrunning(self,ev):
def stoprunning(self,ev):

将发送事件绑定到子例程(http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm)。

替代,您可以将绑定描述为

w.bind('<KeyPress-s>',lambda ev:self.startrunning())
w.bind('<KeyPress-l>',lambda ev:self.stoprunning())
于 2013-03-18T09:23:54.450 回答