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

M

Matthew Thorley

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
 
D

Diez B. Roggisch

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.
 
M

Matthew Thorley

Diez said:
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
 
D

Diez B. Roggisch

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()
 
M

Matthew Thorley

Diez said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top