[Tkinter] Lambda function with indexed list

Post here if you need help with creating a Graphical User Interface in Python.

[Tkinter] Lambda function with indexed list

Postby Pixel_Outlaw » Sat Jun 29, 2013 6:26 am

Hello currently trying to get Tkinter to call an event with two parameters.
The first is the parent widget and the second is a date_data instance within a list.

The following is my main file cal.py.

Note that the container to store date_data objects is on lines 15 and 19.
It is meant to be global.

You'll note that it calls messageWindow(parent, day) on line 60
From the file day_form.py

Here is the 3rd file: day_data.py

The problem here is that it always refers to the last day_data in the global list (uses variable r for index).

To observe the behavior, simply download all 3 files, and run cal.py.
When you click on a day take a look at the title bar of the edit window.

So my lambda function is always getting the last value the index had "r" after all iterations.
I need each button to call the right index instead of the last value r was.
Posts: 2
Joined: Fri Jun 28, 2013 11:15 pm

Re: [Tkinter] Lambda function with indexed list

Postby Pixel_Outlaw » Sun Jun 30, 2013 5:10 am

I was able to find a "hack" to solve it.
lambda r=r: messageWindow(root, cur_days[r])

The problem stemmed from lambda's odd variable scope notions. They don't capture scope from the time of their definition (when r was a different value). Instead, they appear to use the scope of all variables at the time they are called. Forcing r to evaluate it's own value at the time of declaration seems to do the trick.
Posts: 2
Joined: Fri Jun 28, 2013 11:15 pm

Return to GUI

Who is online

Users browsing this forum: Bing [Bot] and 1 guest