简体   繁体   中英

`Extra parameters given` error in tkinter

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.

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM