有一个清单:
liste_physical_activity.insert(1, pa1)
liste_physical_activity.insert(2, pa2)
liste_physical_activity.bind('<<ListboxSelect>>', CurSelet_physical_activity)
liste_physical_activity.pack()
链接到以下功能:
def CurSelet_physical_activity(event, window_mother):
# stuff
即使使用lambda它也不起作用:
<<ListboxSelect>>', lambda event,
window_mother=main_window CurSelet_physical_activity (event, window_mother))
问题是main_window
已在另一个file.py
中创建,因此他不知道。
我该如何解决这个问题?
编辑参考问题:
main.py
from Energy_Requirement import*
main_window =Tk()
bouton_energy_requirement= Button(main_window, text="Estimation of energy requirement", command=lambda:energy_requirement(main_window))
bouton_energy_requirement.pack()
file1.py
def energy_requirement(window_mother):
pa1="NRC"
pa2="Kornfeld"
window5=Toplevel(window_mother)
liste_physical_activity = Listbox(window5,width=80, height=5)
liste_physical_activity.insert(1, pa1)
liste_physical_activity.insert(2, pa2)
liste_physical_activity.bind('<<ListboxSelect>>', CurSelet_physical_activity)
liste_physical_activity.pack()
def CurSelet_physical_activity(event):
global liste_physical_activity
value=str(liste_physical_activity.get(liste_physical_activity.curselection()))
if value==pa1:
ER=1
label = Label(main_window, text="Energy Requirement (kcal ME/day)")
label.pack()
show_ER=StringVar()
show_ER.set(ER)
entree_ER = Entry(main_window,textvariable=show_ER,width=30)
entree_ER.pack()
if value==pa2:
ER=2
label = Label(main_window, text=" Energy Requirement (kcal ME/day)")
label.pack()
show_ER=StringVar()
show_ER.set(ER)
entree_ER = Entry(main_window,textvariable=show_ER,width=30)
entree_ER.pack()
答案 0 :(得分:1)
energy_requirement
正在传递对main_window
的引用,因此您需要做的就是在绑定中传递该值。这应该有效:
def energy_requirement(window_mother):
...
liste_physical_activity.bind('<<ListboxSelect>>',
lambda event, mw=window_mother: CurSelet_physical_activity(event, mw))
然后,您需要修改CurSelet_physical_activity
以接受此附加参数:
def CurSelet_physical_activity(event, main_window):
...
if value==pa1:
ER=1
label = Label(main_window, text="Energy Requirement (kcal ME/day)")
...
看起来您在event
中的任何位置都使用CurSelet_physical_activity
,因此您可以根据需要从绑定和函数的参数列表中删除它。