473,320 Members | 1,926 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

problem with tk and pass by refference (I think :)

Greetings, Maybe someone out there can lend me an eye? I've been
stumped, banging my head against the wall trying to figure out why my
script doesn't work. I've tried every thing I could think of, even
unecessarily complicated mumbo-jumbo. Let me show you a snippet and then
I'll explain what's happening.
for verse in self.activeSong['verses']:
verseNum = self.activeSong['verses'].index(verse)
activeSong = self.activeSong.copy()
firstLine = split(verse, '\n')[0]
button = Button(self.songWin, text=verse, command=(lambda:
self.showVerse(verseNum)) )
button.config(bg='grey')
button.pack(expand=YES, fill=BOTH, side=TOP)
self.verseButtons.append(button)
This is as simple app for displaying the words of a song with an
overhead projector. When you click on a song the program reads it and
creates a button for each verse. When you click the button it is
supposed to display that verse. As you can see above I am trying to call
the showVerse method and pass it the verseNum.

The problem I am having is that every button gets assigned the verseNum
for the last verse that gets processed. That is to say, if a sone has 4
verses every button shows verse for, a 6 verse song loads verse 6 for
every button, etc. I think that the value is getting passed by
reference, so it gets updated with every iteration. I have seriously
tried every thing I can think of to fix this.

If any one has any thoughts I would really appreciate it.
Thanks very much!
-Matthew
Jul 18 '05 #1
4 1226
Hi,

button = Button(self.songWin, text=verse, command=(lambda num=verseNum:
self.showVerse(num)) )

should do the trick. The reason is basically that your version kept a
reference to verseNum - and when executed, the value verseNum points to is
the lasts one stored.

Rebinding the argument to a parameter in the lambda will keep the right
value for each iteration.
--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Diez B. Roggisch wrote:
Hi,

button = Button(self.songWin, text=verse, command=(lambda num=verseNum:
self.showVerse(num)) )

should do the trick. The reason is basically that your version kept a
reference to verseNum - and when executed, the value verseNum points to is
the lasts one stored.

Rebinding the argument to a parameter in the lambda will keep the right
value for each iteration.

I tried it but I got a syntax error. The interpreter didn't like the
equals sign in the lambda. I am using python 2.3.4. Is there another way
of writing that?

thanks
Jul 18 '05 #3
> I tried it but I got a syntax error. The interpreter didn't like the
equals sign in the lambda. I am using python 2.3.4. Is there another way
of writing that?


Strange. This script works and shows the desired behaviour - python is also
2.3.4:

def foo(x):
print x
fs = [lambda: foo(i) for i in xrange(5)]
for f in fs:
f()

fs = [lambda x=i: foo(x) for i in xrange(5)]
for f in fs:
f()
--
Regards,

Diez B. Roggisch
Jul 18 '05 #4
Diez B. Roggisch wrote:
I tried it but I got a syntax error. The interpreter didn't like the
equals sign in the lambda. I am using python 2.3.4. Is there another way
of writing that?

Strange. This script works and shows the desired behaviour - python is also
2.3.4:

def foo(x):
print x
fs = [lambda: foo(i) for i in xrange(5)]
for f in fs:
f()

fs = [lambda x=i: foo(x) for i in xrange(5)]
for f in fs:
f()

You're right! It was my fault. I left the colon after the lambda by
mistake. e.g. lambda: num=verseNum:...

Thanks very much for the help it works great now! I wish I would have
asked somebody sooner :)
-Matthew
Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ralph Freshour | last post by:
I'm trying to dynamically load a .jpg image into a html form <Img tag using a PHP variable that holds the filename of the image but no image is displaying (the file is there and works because if I...
9
by: chris vettese | last post by:
On my subform I have a field in the footer that totals the value of a field. On the main form I have referenced this field. I'm using this field in a calculation on my main form. The problem...
1
by: shade1383 | last post by:
I have a web service that runs fine on my local machine and works fine with a VB .Net project remotely, but when I try to run using Access 2002 I get the following error. Server was unable to...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
13
by: Simon Matthews | last post by:
I am having issues with the right way to architecture the following (using c# asp.net):- The question I have is how best pass the collected data from one web page for use in another. The...
7
by: Lae. | last post by:
I can't figure this one out. n00b question no doubt, this is my first ever JS attempt. Here's the snippet, and here's the full deal http://www.ualberta.ca/~koryb/first.js...
12
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL...
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.