2013-04-25 65 views
11

我是一個Python編程新手,我仍然試圖找出使用lambda。經過一番google搜索我想,我需要用這個按鈕來工作,因爲我需要它TypeError:<lambda>()不需要參數(1給出)

這個作品

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

但worrking一些GUI程序,當我做同樣的縮放不起作用

leds = Scale(root,from_=0,to=255, orient=HORIZONTAL,state=DISABLED,variable =num,command =lambda:scale_changed('LED')) 

回答

29

Scale呼籲爲command傳遞一個參數的函數,所以你必須使用它(儘管日立即排除它)。

變化:

command=lambda: scale_changed('LED') 

command=lambda x: scale_changed('LED') 
+12

'_'傳統上用作損傷'unused argument':'command = lambda _:scale_changed('LED')' – monoid 2013-04-25 13:17:55

4

推測這是因爲該命令被傳遞,也許你不想爭論。嘗試從

command=lambda:scale_changed('LED') 

改變lambda來

command=lambda x:scale_changed('LED') 
2

您應該諮詢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.

更改lambda

command=lambda new_scale_val: scale_changed('LED') 
相關問題