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

Matthew Thorley ruach at chpc.utah.edu
Fri Feb 11 13:27:52 EST 2005


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



More information about the Python-list mailing list