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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:28:47+00:00 2026-06-05T19:28:47+00:00

I have a program which dynamically generates a GUI. I don’t know how many

  • 0

I have a program which dynamically generates a GUI. I don’t know how many buttons I will have, if any at all.

The specific problem is something like this:

for varname in self.filetextboxes:
    if self.varDict[varname]=='':
        self.varDict[varname] = (StringVar(),)
        self.varDict[varname][0].set('')

    fileButton = Button(self, text=" ", command = lambda:self.varDict[varname][0].set(tkFileDialog.askopenfilename()), image=self.filephoto)

    ftb = Entry(self, textvariable = self.varDict[varname][0],width=40,background='white')

I have a for loop which creates the textboxes and the buttons.
The StringVar() is stored in a dictionary with key varname.

because I can’t pass arguments in the callback function of the button, I instead define a lambda in each button. This sets the StringVar() associated with the textbox created in this loop to the output of a filedialog box.

The problem is, the varname passed to the lambda isn’t passing the value, but the name of the variable only. So while the textboxes are associated with the variable they were created with in the for loop, lambdas in the buttons uses the current value of varname at all times.

In other words, each textbox links to only one variable, but all the buttons only set the text of the final textbox created, i.e. the textbox with the final value of varname.

Is there another way to approach this? Can I make the lambda somehow only use the value of varname as it is defined, and not use future values of varname?

  • 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-06-05T19:28:50+00:00Added an answer on June 5, 2026 at 7:28 pm

    I have to admit — this one has me a little bit stumped. Since you’re pairing a Button with an Entry and stringvar, one workaround is to wrap those together in a class. (I could probably argue that this is a little more elegant anyway…)

    import Tkinter as tk
    
    class ButtonEntry(tk.Frame):
        def __init__(self,master,ss):
            tk.Frame.__init__(self)
            self.var=tk.StringVar()
            self.var.set(ss)
            self.Button=tk.Button(self,text='Button',command=lambda :self.var.set("foo!"))
            self.Entry=tk.Entry(self,textvariable=self.var)
            self.Button.grid(row=0,column=0)
            self.Entry.grid(row=0,column=1)
    
    
    class App(tk.Frame):
        def __init__(self,master=None):
            tk.Frame.__init__(self,master)
            self.BEs=[]
            for i in range(10):
                b=ButtonEntry(self,'Button %d'%i)
                b.grid(row=i,column=0)
                self.BEs.append(b)
    
    
    if __name__ == '__main__':
        root=tk.Tk()
        f=App(root)
        f.grid(row=0,column=0)
        root.mainloop()
    

    However, I would really love to know why the lambda behaves the way it does. I thought for sure what you had should work.

    EDIT

    I’ve tracked down the behavior of the lambda function. https://stackoverflow.com/a/10452819/748858 gives a great example of how to do this properly. The problem is that the variables in the lambda function are still bound to the scope where the lambda was declared. In order to disassociate them with that scope, you need to set them as a keyword argument to the function. Nice! I learned something new this morning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program which will dynamically set an iframe src to load pages.
I have coded a program which dynamically inserts nodes and edges, overlaying the graph
I have a program in which I need to run multiple insert statements (dynamically
I have a program which creates certain save files during its use. Technically they
I have a program which I want to use keybindings: // Imports.. public class
I have a program which I'm trying to get to work as fast as
I have a program which I would like to run every X min the
I have a program which runs an external, command line utility and reads the
I have a program which doesn't once reference System.I0, coded in vb.net, yet for
I have a program which takes a long time to complete. I would like

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.