【问题标题】:Bind a tkinter widget to a function containing args - Use of Lambda将 tkinter 小部件绑定到包含 args 的函数 - Lambda 的使用
【发布时间】:2015-03-11 19:14:49
【问题描述】:

有一个清单:

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中创建了,所以他不知道。

我该如何解决这个问题?

编辑参考问题:

ma​​in.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()

【问题讨论】:

  • 对 lambda 的使用看起来不像是有效的 python。那是你的实际代码吗?
  • 我不使用它,因为它不起作用并返回“未定义主窗口”,但我试图从中适应:stackoverflow.com/questions/7299955/…
  • 在没有看到实际代码的情况下,无法猜测如何修复它。请提供一个完整的、最小的示例来说明问题。不要包含所有代码,创建最小的工作程序可能会产生相同的错误。
  • 再次编辑。希望这会有所帮助。非常感谢。

标签: python lambda tkinter


【解决方案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)")
       ...

您似乎没有在CurSelet_physical_activity 的任何地方使用event,因此您可以根据需要从绑定和函数的参数列表中删除它。

【讨论】:

  • 谢谢。了解 lambda 的使用。我编辑我的帖子来解释这个主窗口问题。 :o
  • 感谢您的回答。仍然不起作用:“ liste_physical_activity.bind('>', lambda event, mw=main_window: CurSelet_physical_activity(event, mw)) NameError: name 'main_window' is not defined "
  • @BoobaGump:我很抱歉。我没认出你在energy_requirement 中称​​它为window_mother。在绑定中更改它,它将起作用。我会更新答案。
猜你喜欢
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多