473,769 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.son gWin, text=verse, command=(lambda :
self.showVerse( verseNum)) )
button.config(b g='grey')
button.pack(exp and=YES, fill=BOTH, side=TOP)
self.verseButto ns.append(butto n)
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 1238
Hi,

button = Button(self.son gWin, 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.son gWin, 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
79139
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 manually type in the filename the <Img tag works) - how can I get PHP's variable to be seen as the filename? Thanks... <?php print '<IMG SRC="$php_photo2_file" WIDTH="268" HEIGHT="176" BORDER="0"
9
3159
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 occurs when there are no records in the subform. The field on the main form that references the subform total field returns #Error. How can I make this field show the total when there are records and 0 when there are no records??? Thank you,...
1
3995
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 proccess request ---> object refference not set to an instance of an object Following is the web method: Public Structure vbVINVariables Public mModel As String Public mYear As String
8
10719
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 receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
23
3083
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 block. But, I prefer to dim them "on the fly" only if needed (save as much resources as possible). A little further... I may wish to create a sqlcommand and datareader object ONLY if certain conditions are met. But, if I want to clean these up in the...
13
1737
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 question I have is how best pass the search criteria from the first page to the second. The search criteria exists as a structure and is filled when the user presses the 'Search' button on the first page.
7
1557
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 http://www.ualberta.ca/~koryb/ http://www.ualberta.ca/~koryb/test.html runs it // Runs timer loop and increments pics function timer()
12
3026
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 function. When I try and compile the DLL I get "The type or namespace name "MyForm" could not be found. I think I have to reference the class but since the DLL needs to be built before the EXE it looks like I have a chicken and egg type problem....
4
9119
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) pass by reference (inother words: call by reference) 3) pass by value-result <- i have question this subject . 4) pass by name
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5298
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.