please help

Paul Simmonds psimmo60 at hotmail.com
Fri Nov 29 09:57:43 EST 2002


<Problem description snipped>
Ok, a couple of points for next time:

   1. Please Please Please try and make sure your code in your email has the 
correct indentation. In a language such as Python, where whitespace matters, 
it makes it hard to figure out whether it's a problem with the code, or the 
indentation. Which leads me onto..

   2. Try and include some sort of description as to what errors you're 
getting. I'm assuming it's the Index error. Well, that's the one I'm going 
to talk about...

<code snipped and reformatted by yours truly into>
class but:
   def __init__(self,master,row,col):
        self.button=Button(master,text = ' =',\                              
           command=self.clicked(row,col))
        self.index = 0                          #<-Index defined here
        self.b = Board()

   def clicked(self,row,col):
        self.index = self.index+1  #<-This line's causing the error
        print self.index
<reformatted code ends>

What you're running into is the same problem I- and a few others, probably- 
have in the past. Try this:

>>>from Tkinter import *
>>>from tkMessageBox import *
>>>Button(text="Run me",command= showwarning("Oops!",\
...       "What happened?")).pack()

This illustrates your problem, as the function is being called on creation, 
not on triggering. This means index is needed in your 'clicked' function 
before it is defined. Now, try this one:

>>>Button(text="Run me",command=lambda: showinfo("Aha!",\
...       "That's much better!")).pack()

This creates a nameless function that is triggered when the button is 
clicked. Lambdas are highly useful in this context.

Hope this helps,
Paul

>
>---------------------------------
>With Yahoo! Mail you can get a bigger mailbox -- choose a size that fits 
>your needs


_________________________________________________________________
MSN 8 with e-mail virus protection service: 2 months FREE* 
http://join.msn.com/?page=features/virus





More information about the Python-list mailing list