看板 Python 關於我們 聯絡資訊
※ 引述《kfrico (霧峰小霸王)》之銘言: : for i in range(0,3): : g[i]=tk.Button(sf,text = '按鈕',command=lambda:myfunction(i)) : g[i].grid(row=row_num,column=3) : 為什麼在myfunction接收參數時都是2 : 是我寫錯那裡嗎? 這裡我覺得用closure 有點難懂。 你可以參考一下functools. import functools def myfun(i): return i commands = [] for i in range(3): commands.append(functools.partial(myfun,i)) for c in commands: print c() # 0, 1, 2 commands = [] for i in range(3): commands.append(lambda :myfun(i)) for c in commands: print c() # 2 2 2 這樣會比較happy 一些 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 219.87.142.18 ※ 編輯: timTan 來自: 219.87.142.18 (01/20 01:53)
kfrico:太感謝你了 01/20 09:40