Reputation: 305
I am trying to bind separate functions to the left and right click of a button. Unfortunately somehow python gives me this error when pressing the button:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in __call__
return self.func(*args)
TypeError: () takes no arguments (1 given)
I think it has something to do with Tkinter also passing event information when calling the function.
This is the Code (I shortened it a bit and removed unnessecary things):
class Minefield():
def __init__(self, X,Y,Parent,Array):
self.Button = Tk.Button(Parent)
self.Button.bind('<Button-1>',lambda: Array.Trigger(self.state,self.cordX,self.cordY) ) # NOTE: Array Function Trigger
self.Button.bind('<Button-3>',lambda: self.setState(self.state,1))
class Array():
def __init__(self):
self.Array=[]
def Trigger(self,X,Y):
print "Trigger at", X, Y
do_nothing()
now my Question:
What parameters effectively get passed and how do I handle them in a function which has no intent of using other things than those defined in the parameters?
Upvotes: 2
Views: 686
Reputation: 386230
When you bind a function to an event, tkinter will always pass one argument to the function. This argument is an object that contains information about the event, such as the widget that received the event, the key or mouse that caused the event, etc.
If you are calling a function that doesn't need the argument, just ignore it. If you need to use the function also outside the context of an event, give it a default value of None
.
If you are passing additional arguments to your callback, you need to account for the event
argument in your lambda. You don't have to use it, but your lambda needs to accept it. For example:
self.Button.bind('<Button-3>', lambda event: self.setState(self.state,1))
Upvotes: 2