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!