1

This is an inline code of tkinter Scaler that bind to a function

self.slidery =tk.Scale(self.valuesframe, from_=-2.000, to=2.000,
    sliderlength=10, tickinterval=1, length=self.windowwidth -self.sliderresizer
    ,variable=1,label="Y",borderwidth=2,resolution=0.01,width=10, highlightthickness=10, digits=3, troughcolor="red", orient=tk.HORIZONTAL)
self.slidery.pack()
self.slidery.bind("<ButtonRelease-1>", self.GetSave("Y", self.slidery.get()))

GetSave( ) function is binded and here is the function :

def GetSave (self,event,opt,val):
    print(opt +" : "+str(val))
    pass 

What I am receiving : TypeError: GetSave() missing 1 required positional argument: 'val'

I change to :

self.slidery.bind("<ButtonRelease-1>", self.GetSave(event,"Y", self.slidery.get()))

Also tried lambda:

self.sliderx.bind("<ButtonRelease-1>",lambda x:self.GetSave("X",self.sliderx.get()))

Still Errors.

self and event auto passed to function as I know . Then what is wrong with my code ?

user7685914
  • 83
  • 1
  • 9
  • It is a very long code. And Yes it is inside a class... Antti – user7685914 Mar 19 '17 at 11:54
  • Also see http://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared and http://stackoverflow.com/questions/6920302/how-to-pass-arguments-to-a-button-command-in-tkinter – PM 2Ring Mar 19 '17 at 12:06
  • Apart from the problems relating to the args of your callback, you have to pass the callback function object itself to `.bind`. You _don't_ call the function in the `.bind` call because then `.bind` just gets whatever your callback returns, not the function itself. The 2 questions I linked go into further details. – PM 2Ring Mar 19 '17 at 12:14
  • Sorry, I had to remove my answer: look to how you defined `GetSave()`: when you call it, it does not take any of the arguments you specified in its definition. You need to inject the required arguments. – Billal Begueradj Mar 19 '17 at 12:18
  • Thank you very much PM. I will take a look at them. – user7685914 Mar 19 '17 at 12:18
  • It is good to see the problem from different perspective with many answers..Thank you Billal .. . – user7685914 Mar 19 '17 at 12:20

1 Answers1

2

Try this, I've slightly changed your lambda

self.sliderx.bind("<ButtonRelease-1>",lambda event: self.GetSave(event, "X",self.sliderx.get()))
Tom Fuller
  • 5,291
  • 7
  • 33
  • 42
  • oh this worked. But what is the logic behind this ? Example why dont we pass .self and we pass event. Cause in a previous function i did not pass event it did not create a problem ..Sorry I still learn stuff . – user7685914 Mar 19 '17 at 11:58
  • With classes `self` is automatically passed. For example in the line `self.GetSave(...)` you've already referenced `self`, so you don't need to pass it again `self.GetSave(self ...)`. I think that event is automatically passed aswell when you use the `.bind` method, unless you use lambda (which 'catches' the event) you don't need to pass event – Tom Fuller Mar 19 '17 at 12:02
  • I think this is is the hardest part of python for me.. Thank you very much Tom. – user7685914 Mar 19 '17 at 12:09
  • 1
    That's because the command parameter in `.bind` takes the function name, for example `window.bind("event", function)` rather than `window.bind("event", function())`. This is why `lambda` needs to be used to pass extra arguments, also no problem :) – Tom Fuller Mar 19 '17 at 12:12