Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:46:18+00:00 2026-05-25T20:46:18+00:00

I am currently working on a GUI (Tkinter) for my application. I am having

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:46:19+00:00Added an answer on May 25, 2026 at 8:46 pm

    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.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have banking gui application that I am currently working on and there seems
I am working on a GUI application that will be used to perform manual
Am currently working on an application that requires users to submit posts and comments
I'm currently working on creating a new C# project that needs to interact with
I'm currently working on a GUI based application and am using the code-generation feature
I am currently working on this big project (a GUI to control hardware) that
I'm currently working on a Windows Forms GUI and I have a Combo that
Currently I am working on a C++/clr application that is slowly making progress. When
I am currently working on a WPF application that processes data in a database
I'm currently working on a robot interface GUI, using C#. The robot has two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.