15

I am a newbie to python programming and am still trying to figure out the use of lambda. Was worrking on some gui program after much googling i figured that i need to use this for buttons to work as i need it to

THIS WORKS

mtrf = Button(root, text = "OFF",state=DISABLED,command = lambda:b_clicked("mtrf"))

but when i do the same for Scale it does not work

leds = Scale(root,from_=0,to=255, orient=HORIZONTAL,state=DISABLED,variable =num,command =lambda:scale_changed('LED'))
jamylak
  • 128,818
  • 30
  • 231
  • 230
evolutionizer
  • 283
  • 2
  • 3
  • 7

3 Answers3

40

Scale calls the function passed as command with one argument, so you have to use it (although throw it away immediately).

Change:

command=lambda: scale_changed('LED')

to

command=lambda x: scale_changed('LED')
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 19
    `_` is traditionally used as marker of 'unused argument': `command=lambda _: scale_changed('LED')` – monoid Apr 25 '13 at 13:17
5

This is presumably because the command is passed an argument that perhaps you don't want. Try changing the lambda from

command=lambda:scale_changed('LED')

to

command=lambda x:scale_changed('LED')
Alex
  • 7,639
  • 3
  • 45
  • 58
2

You should consult Tkinter documentation:

Scale widget

command - A procedure to be called every time the slider is moved. This procedure will be passed one argument, the new scale value. If the slider is moved rapidly, you may not get a callback for every possible position, but you'll certainly get a callback when it settles.


Button widget

command - Function or method to be called when the button is clicked.

Change your lambda to

command=lambda new_scale_val: scale_changed('LED')
jamylak
  • 128,818
  • 30
  • 231
  • 230