Python:Tkinter 菜单条目未传递正确的值

发布于 2024-12-06 05:54:51 字数 877 浏览 0 评论 0原文

我目前正在为我的应用程序开发 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-12-13 05:54:51

命令更改为:

lambda the_year=the_year: year_seter(the_year)

该问题与Python如何查找the_year的值有关。
当您使用

lambda : year_seter(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


附言。

  1. 您也可以放弃定义 year_seter 并执行以下操作:

    lambda the_year=the_year:year.set(the_year)
    
  2. list_of_years = range(1995,2012) 也可以。

Change the command to:

lambda the_year=the_year: year_seter(the_year)

The problem has to do with how Python looks up the value of the_year.
When you use

lambda : year_seter(the_year)

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. The for-loop uses the_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 to the_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 for the_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 to the_year.


PS.

  1. You could also forgo defining year_seter and just do:

    lambda the_year=the_year: year.set(the_year)
    
  2. list_of_years = range(1995,2012) also works.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文