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

  • Home
  • SEARCH
  • 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 7787265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:36:32+00:00 2026-06-01T20:36:32+00:00

Windows 7×64 Python2.7 I’m looking to apply the Lexical closures in Python thread solution

  • 0

Windows 7×64 Python2.7

I’m looking to apply the Lexical closures in Python thread solution to my code. Additional readings Closures in a for loop and lexical environment

I go through a list of image, thumbnail pairs, and display the thumbnail, and upon clicking the label, it should show a new TopLevel with the full sized image in it.

What actually happens is that it is only the last image of the ImagePairs is shown. Searching around, I found that thread I posted above, but I’m uncertain about how exactly to apply it to my situation.

            row, col = 0, 0
            #create a frame for the row
            rowFrame = Frame(master)
            for image, thumb in ImagePairs:
                curLbl = Label(rowFrame, image=thumb)
                curLbl.grid(row=0, column=col, sticky='news')

                curLbl.bind('<Button-1>', lambda e:self.popImage(image))  #or curLbl.image

                col += 1
                if col >= 3:
                    rowFrame.grid(row=row)
                    #create a new frame, for the next row
                    rowFrame = Frame(master)
                    col = 0
                    row += 1

I figured making a function like

def func(img=img):
   return img

and insert it in the space between grid() and bind(), but then I get the error for a missing image. How can I modify my code to fit that in?

  • 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-01T20:36:34+00:00Added an answer on June 1, 2026 at 8:36 pm

    You can add the default value image = image to the lambda itself:

    for image, thumb in ImagePairs:
                curLbl = Label(rowFrame, image=thumb)
                curLbl.grid(row=0, column=col, sticky='news')
                curLbl.bind('<Button-1>', lambda e, image = image:self.popImage(image))  #or curLbl.image
    

    Your idea of defining func would also work if you made the default value image instead of img:

    def func(e, img = image):
        self.popImage(img)
    

    The key thing to remember is that default values are bound at the time the lambda (or function) is defined.

    So each time through the loop, a new lambda is defined with its own local variable image, with its own default value for image bound at definition-time. Thus, when the lambda is called later, the right image is accessed.

    Contrast that with the situation when no default value is supplied: image is no longer a local variable of the lambda. When the lambda is called, Python goes looking for it in the enclosing scope. But by then the for-loop is done and image in the enclosing scope is set to the last value in ImagePairs.
    That is why you were getting the same last image no matter what button you pressed: all the lambdas were accessing the value of the same variable image.

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

Sidebar

Related Questions

I'm running Google Chrome 8.0.552.215 on Windows 7 x64 Ultimate. I have a simple
I have written a small example C++ program, using boost::thread. Since it's 215 lines,
the following is my code. Editor: Eclipse, Platform: Windows. Its a chat application where
Windows 2003/IIS 6... I have a virtual directory on a web site that closely
Windows XP start menu is divided into two parts, the left and the right.
Windows recognizes and gives my fedora partition a drive letter, but it shows it
Windows provides only GetTickCount up to Windows Vista and starting from that OS also
Windows 7 has a very nifty way of showing Contributing Artist metadata in Windows
Windows has a Performance Counters infrastructure. The OS provides some common counters, and I
Windows XP SP3. Core 2 Duo 2.0 GHz. I'm finding the boost::lexical_cast performance to

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.