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 166445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:05:56+00:00 2026-05-11T12:05:56+00:00

I have a menubutton, which when clicked should display a menu containing a specific

  • 0

I have a menubutton, which when clicked should display a menu containing a specific sequence of strings. Exactly what strings are in that sequence, we do not know until runtime, so the menu that pops up must be generated at that moment. Here’s what I have:

class para_frame(Frame):     def __init__(self, para=None, *args, **kwargs):         # ...          # menu button for adding tags that already exist in other para's         self.add_tag_mb = Menubutton(self, text='Add tags...')          # this menu needs to re-create itself every time it's clicked         self.add_tag_menu = Menu(self.add_tag_mb,                                  tearoff=0,                                  postcommand = self.build_add_tag_menu)          self.add_tag_mb['menu'] = self.add_tag_menu      # ...      def build_add_tag_menu(self):         self.add_tag_menu.delete(0, END) # clear whatever was in the menu before          all_tags = self.get_article().all_tags()         # we don't want the menu to include tags that already in this para         menu_tags = [tag for tag in all_tags if tag not in self.para.tags]          if menu_tags:             for tag in menu_tags:                 def new_command():                     self.add_tag(tag)                  self.add_tag_menu.add_command(label = tag,                                               command = new_command)         else:             self.add_tag_menu.add_command(label = '<No tags>') 

The important part is the stuff under ‘if menu_tags:’ — Suppose menu_tags is the list [‘stack’, ‘over’, ‘flow’]. Then what I want to do is effectively this:

self.add_tag_menu.add_command(label = 'stack', command = add_tag_stack) self.add_tag_menu.add_command(label = 'over', command = add_tag_over) self.add_tag_menu.add_command(label = 'flow', command = add_tag_flow) 

where add_tag_stack() is defined as:

def add_tag_stack():     self.add_tag('stack') 

and so on.

The problem is, the variable ‘tag’ takes on the value ‘stack’ and then the value ‘over’ and so on, and it doesn’t get evaluated until new_command is called, at which point the variable ‘tag’ is just ‘flow’. So the tag that gets added is always the last one on the menu, no matter what the user clicks on.

I was originally using a lambda, and I thought maybe explicitly defining the function as above might work better. Either way the problem occurs. I’ve tried using a copy of the variable ‘tag’ (either with ‘current_tag = tag’ or using the copy module) but that doesn’t solve it. I’m not sure why.

My mind is starting to wander towards things like ‘eval’ but I’m hoping someone can think of a clever way that doesn’t involve such horrible things.

Much thanks!

(In case it’s relevant, Tkinter.__version__ returns ‘$Revision: 67083 $’ and I’m using Python 2.6.1 on Windows XP.)

  • 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. 2026-05-11T12:05:57+00:00Added an answer on May 11, 2026 at 12:05 pm

    First of all, your problem doesn’t have anything to do with Tkinter; it’s best if you reduce it down to a simple piece of code demonstrating your problem, so you can experiment with it more easily. Here’s a simplified version of what you’re doing that I experimented with. I’m substituting a dict in place of the menu, to make it easy to write a small test case.

    items = ['stack', 'over', 'flow'] map = { }  for item in items:     def new_command():         print(item)      map[item] = new_command  map['stack']() map['over']() map['flow']() 

    Now, when we execute this, as you said, we get:

    flow flow flow 

    The issue here is Python’s notion of scope. In particular, the for statement does not introduce a new level of scope, nor a new binding for item; so it is updating the same item variable each time through the loop, and all of the new_command() functions are referring to that same item.

    What you need to do is introduce a new level of scope, with a new binding, for each of the items. The easiest way to do that is to wrap it in a new function definition:

    for item in items:     def item_command(name):         def new_command():             print(name)         return new_command      map[item] = item_command(item) 

    Now, if you substitute that into the preceding program, you get the desired result:

    stack over flow 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You'll have to get rid of the left recursion. This… May 11, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer Eclipse is the best IDE to use for both Python… May 11, 2026 at 11:39 pm
  • Editorial Team
    Editorial Team added an answer .csproj, assemblyinfo.cs, plus your code and solution (.sln) file. Also,… May 11, 2026 at 11:39 pm

Related Questions

I was wondering if anyone knew of a way to do some client side
is there any way to make IE6 understand double classes, say I have a
I am trying to pass a member function within a class to a function
I have a web-service that I will be deploying to dev, staging and production.

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.