Python:Tkinter 菜单条目未传递正确的值
我目前正在为我的应用程序开发 GUI (Tkinter)。我在创建几个用于选择日期的下拉菜单时遇到问题。我编写的应用程序创建了带有标签的所需菜单,但是,通过单击任何按钮,只有最后一个菜单条目的值会传递到 tkinter 可变 IntVar。
这是强调我的问题的代码的一部分。 Year 应该是用户点击的年份,但是,它始终是 2011 年。
from Tkinter import *
import tkFileDialog as dialog
import datetime
import calendar
window = Tk()
text = Text(window)
text.pack()
year = IntVar()
list_of_years = [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011]
def year_seter(value):
year.set(value)
menubar = Menu(window)
yearmenu = Menu(menubar)
for the_year in list_of_years:
yearmenu.add_command(label=str(the_year), command=lambda : year_seter(the_year))
menubar.add_cascade(label = 'Year', menu=yearmenu)
window.config(menu=menubar)
label = Label(window, textvariable=year)
label.pack()
window.mainloop()
有人可以向我解释一下,为什么会发生这种情况吗? 谢谢您的宝贵时间!
I am currently working on a GUI (Tkinter) for my application. I am having problems with creating a couple of dropdown menus that should be used to choose a date. The application that I have written creates the desired menus with labels, however, by clicking any of the buttons only the value of the last menu entry gets passed to the tkinter mutable IntVar.
This is a portion of the code that emphasizes my problem. year should be the year that the user clicks upon, however, it is always 2011.
from Tkinter import *
import tkFileDialog as dialog
import datetime
import calendar
window = Tk()
text = Text(window)
text.pack()
year = IntVar()
list_of_years = [1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011]
def year_seter(value):
year.set(value)
menubar = Menu(window)
yearmenu = Menu(menubar)
for the_year in list_of_years:
yearmenu.add_command(label=str(the_year), command=lambda : year_seter(the_year))
menubar.add_cascade(label = 'Year', menu=yearmenu)
window.config(menu=menubar)
label = Label(window, textvariable=year)
label.pack()
window.mainloop()
Can somebody please explain to me, why is this happening?
Thank you for your time!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将
命令
更改为:该问题与Python如何查找
the_year
的值有关。当您使用
the_year
时,它不在 lambda 函数的本地范围内,因此 Python 会在 扩展、全局、内置范围。它在全局范围内找到它。for 循环
使用the_year
,在 for 循环结束后,the_year
保留其最后的值 2011。由于执行了 lambda 函数for 循环结束后,Python 分配给the_year
的值是 2011。相反,如果您使用具有默认值的参数,则默认值在函数 (lambda) 运行时固定是定义。因此,每个 lambda 都会获得固定为默认值的不同的 the_year 值。
现在,当调用 lambda 时,Python 再次查找 the_year 的值,但这次是在 lambda 的本地作用域中找到它。它将默认值绑定到
the_year
。附言。
您也可以放弃定义
year_seter
并执行以下操作:list_of_years = range(1995,2012)
也可以。Change the
command
to:The problem has to do with how Python looks up the value of
the_year
.When you use
the_year
is not in the local scope of the lambda function, so Python goes looking for it in the extended, global, then builtin scopes. It finds it in the global scope. Thefor-loop
usesthe_year
, and after the for-loop ends,the_year
retains its last value, 2011. Since the lambda function is executed after the for-loop has ended, the value Python assigns tothe_year
is 2011.In contrast, if you use a parameter with a default value, the default value is fixed at the time the function (lambda) is defined. Thus, each
lambda
gets a different value forthe_year
fixed as the default value.Now when the lambda is called, again Python goes looking for the value of
the_year
, but this time finds it in the lambda's local scope. It binds the default value tothe_year
.PS.
You could also forgo defining
year_seter
and just do:list_of_years = range(1995,2012)
also works.