1

I'm trying to fix up a little program, but looks like I'm in a bit over my head. The whole code is too long to copy here, so I'm just gonna paste the problematic part.

def kontroll(self):
    count=IntVar()
    sisend=sisendivaartus.get()
    print(count)
    if count==1:
        kast.delete(0.0,END)
        sisend.delete(0.0,END)
        count=0
    else:
        kast.delete(0.0,END)
        if sisend=="õige" or sisend=="ÕIGE" or sisend=="Õige":
            if oige==sonake:
                if tahendus==" ":
                    kast.insert(END,"Tubli, õige!"+"\n"+str(oige)+"\n"+str(tahendus))
                    count=1
                else:
                    kast.insert(END,"Tubli, õige!"+"\n"+str(oige)+"\n:"+str(tahendus))
                    count=1
                #skoor+=1
                #skoor=skoor+1
            else:
                if tahendus==" ":
                    kast.insert(END,"Kahjuks eksid. Õige on "+str(oige)+"\n"+str(tahendus))
                    count=1
                else:
                    kast.insert(END,"Kahjuks eksid. Õige on "+str(oige)+":\n"+str(tahendus))
                    count=1
                #vale=vale+1
                #skoor=skoor-1
        else:
            if sisend==oige:
                if tahendus==" ":
                    kast.insert(END,"Tubli, õige!\n"+str(oige)+"\n"+str(tahendus))
                    count=1
                else:
                    kast.insert(END,"Tubli, õige!\n"+str(oige)+":\n"+str(tahendus))
                    count=1
                #skoor=skoor+1
            else:
                if tahendus==" ":
                    kast.insert(END,"Kahjuks eksid. Õige on "+str(oige)+"\n"+str(tahendus))
                    count=1
                else:
                    kast.insert(END,"Kahjuks eksid. Õige on "+str(oige)+":\n"+str(tahendus))
                    count=1
                #vale=vale+1
                #skoor=skoor-1
        #if skoor<0:
         #   skoor=0

Now the problem is that I have to add arguments to kontroll() but I don't know how to do that with 'self' being there. To be honest, I don't even understand why I need that 'self' there, 'cause I'm not using classes which usually use the 'self' argument... So anyway, I googled the error I recieved without having 'self' there and after adding 'self' it started working, but I can't add 'skoor' and 'vale' in brackets... Right now I just start kontroll with nothing in brackets, like this: kontroll(). So how should I start the function and how should I add the arguments to it? I can't use global variables either, 'cause this 'skoor=skoor+1' messes everything up (can't have global variable on the left side of equals sign).

Thanks.

EDIT: GUI part of my program looks like this:

raam=Tk()
raam.title("MÄNG")
raam.geometry("430x450")
valik=IntVar()
valik2=IntVar()
C2=Checkbutton(raam,text="f",variable=valik2,command=lambda:ok.config(state=ACTIVE)).place(x=5,y=10)
ok=Button(raam,text="Alusta!",command=alusta,state=DISABLED)
ok.place(x=230,y=20,width=80)
C1=Checkbutton(raam,text="š",variable=valik,command=lambda:ok.config(state=ACTIVE)).place(x=5,y=30)
sisendivaartus=StringVar()
sisend=Entry(raam,textvariable=sisendivaartus)
sisend.place(x=10,y=250,width=200)
sisend.bind("<Return>",kontroll(skoor,vale))
credit=Label(raam,text="2013",font=("Verdana","7"))
credit.place(x=0,y=430)
score=Label(raam,text="Skoor:",font=("Verdana","7"))
score.place(x=380,y=430)
nupp=Button(raam,text="Seaded",command=seaded)
nupp.config()
nupp.place(x=330,y=20,width=80)
kast=Text(raam,wrap=WORD,font=("Verdana",10))
kast.config()
app=App(raam)
kast.place(x=10,y=60,width=400,height=180)
raam.mainloop()

For Henry's suggestion (how to use the function when not in class), I start getting different errors that have no basis, for example "NameError: global name 'oige' is not defined". Despite what the error says, global name 'oige' is defined and works fine without arguments. If I replace 'skoor' and 'vale' in the functions definition with just 'self' and recall it without any arguments, everything works just fine. I guess this is kind of hard to understand without seeing the whole piece, so I uploaded it here, if anyone has a minute. PS. what I said earlier about having 0 classes in my code - that has changed since I constantly keep working on this. Also please keep in mind that this is a beta version of a beta, so many things are not supposed to work yet. (For trivia: it tests and trains person's capabilities in Estonian orthography)

makaveli
  • 69
  • 1
  • 9
  • 1
    Please check your indentation. Python is whitespace sensitive. – thegrinner Apr 03 '13 at 15:26
  • 1
    If this isn't an instance method, you absolutely don't need `self` there. How were you calling the function when you got the error message? – Wooble Apr 03 '13 at 15:30
  • Your code is nearly impossible to read; you should have a look at [PEP8](http://www.python.org/dev/peps/pep-0008/). I found a variety of problems with a brief glance. You should not be declaring `skoor` and `vale` as global. Your specific problem with calling `kontroll` is that you actually want to bind it to a `tkinter` `Entry`, but you're calling it yourself instead. You want something like `sisend.bind("", kontroll)` instead, but I haven't messed used `tkinter` and can't help you more than that. Either way, binding functions to `tkinter` objects belongs in a separate question. – Henry Keiter Apr 04 '13 at 17:31
  • Yes, I believe it might be for a person who hasn't worked it and doesn't know Estonian... Sorry. :( I will delve into that reference before continuing with my project. Thanks for all your help and for detecting the problematic part. I'm not yet sure what to do with it, but I'll come up with something. :) As you definitely have gathered, I'm still far from a real programmer and this is actually a high school project. My gratitude for all your help and hopefully one day both me and this program will function at a more satisfacting rate! – makaveli Apr 04 '13 at 18:10
  • @user2241069 For the record, it's not really the language that's an issue. There just aren't any spaces between lines or syntactical elements, which makes everything look very cluttered. Even just putting spaces around your `=` signs would make a big difference. – Henry Keiter Apr 04 '13 at 19:04

2 Answers2

5

Writing Your Function

It's very easy to add more arguments to a function; simply change your function definition to

def kontroll(self, skoor, vale):
    # Your code here.

Then skoor and vale become variable names local to the kontroll function and you can use them like any other variable.

As to why you need self in the function definition, that's because (presumably) this method is actually an instance method of some class you've defined. Instance methods always receive the object they're bound to as their first argument. This argument is called self, which is why you have to have an argument reserved for it. See this question for a more complete explanation.

If this is not actually an instance method of some class, mind you, then you don't actually need self. And generally speaking, if the method doesn't need a reference to a specific object instance, then there's no reason to make it an instance method!

Calling Your Function

Calling your function is just as simple, but it depends on the answer to the other question: is this actually an instance method? If it's defined inside a class (which it sounds like it is) then you need to do something like the following:

class C(object):
    def kontroll(self, skoor, vale):
        pass # Your code goes here.

c = C() # Create an instance of the class!
c.kontroll(skoor, vale) # The instance (c) is automatically passed to the method.

If, on the other hand, your function is not defined inside a class, your code should look like this:

def kontroll(skoor, vale):
    pass # Your code goes here.

kontroll(skoor, vale)
Community
  • 1
  • 1
Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
  • Thanks for answers, but how do I call kontroll then? It won't work with neither kontroll(self,skoor,vale) nor kontroll(skoor,vale). Error for both instances: TypeError: kontroll() missing 2 required positional arguments: 'skoor' and 'vale' – makaveli Apr 03 '13 at 17:23
  • @user2241069 You need to tell us where the function is. I've edited my answer; look at the second half for how to call the function, depending on how your code is set up (and read the [link](http://stackoverflow.com/questions/2709821/python-self-explained) I posted to **Python 'self' explained**!) – Henry Keiter Apr 03 '13 at 17:30
  • I have not **knowingly** placed the function inside a class - what I mean is I have 0 classes in my whole code and so I assume it is located outside of a class. If there exists another more lucrative way for things to end up in a class, please explain. In short, the function is not in a class. I read the link about 'self', thank you. I had already read about it previously and I think I **maybe** understand it's purpose in a class and when I have __init__ present and whatnot but my code should be much more simple and not require it. That's why I'm asking. It doesn't work with your propositions. – makaveli Apr 04 '13 at 07:07
  • @user2241069 "It doesn't work" doesn't give anyone enough information to be able to help you. Did you define it and call it the way my answer indicates you should "if your function is not defined inside a class"? What errors are you getting? What does your code look like? I can't possibly help unless you tell me these things. – Henry Keiter Apr 04 '13 at 13:34
  • I added to my original question, hopefully that can shed some light on the matter. I can't thank you enough for answering my silly questions! – makaveli Apr 04 '13 at 16:47
1

You need self because this is almost certainly a method of a class, and you need to pass the object to the method when it is called. Self is just a convention, technically you can use some other name for this just as easily. That being said, it seems like you need to do some reading about classes in python and really try to understand the way an object instance works. I recommend starting with the docs:

http://docs.python.org/2/tutorial/classes.html

There appear to be a lot of globals in your code snippet that should probably be class variables, and you really need to understand how things like the __init__ method work before going to deep into this.

Justin.Wood
  • 695
  • 4
  • 10