Bind a tkinter widget to a function containing args - Use of Lambda

前端 未结 1 1872
逝去的感伤
逝去的感伤 2021-01-28 10:55

There is a list :

liste_physical_activity.insert(1, pa1)
liste_physical_activity.insert(2, pa2)
liste_physical_activity.bind(\'<>\',          


        
相关标签:
1条回答
  • 2021-01-28 11:56

    energy_requirement is being passed a reference to main_window, so all you need to do is pass that value along in the binding. This should work:

    def energy_requirement(window_mother):
        ...
        liste_physical_activity.bind('<<ListboxSelect>>',  
            lambda event, mw=window_mother: CurSelet_physical_activity(event, mw))
    

    You'll then need to modify CurSelet_physical_activity to accept this additional parameter:

    def CurSelet_physical_activity(event, main_window):
        ...
    
        if value==pa1:
           ER=1
           label = Label(main_window, text="Energy Requirement (kcal ME/day)")
           ...
    

    It doesn't look like you use event anywhere in CurSelet_physical_activity, so you can remove that from the binding and from the parameter list of the function if you want.

    0 讨论(0)
提交回复
热议问题