0

So I am trying to create a GUI that shares information between frames (which are set up as classes) and ran into this error, which despite the many hours of googling and trying different things, had no luck solving. Im just trying to create a library which can be written to and read from other classes. I created this test code just to debug the problem so if you want to see my main code let me know, thanks.

class Vars():

    global vari 

    vari = dict(Sifo = False, Username = "", Password = "", Event = "", Time = "")


    def GetVars(self, var):

        print "1"
        return vari.pop(var)

    def SendVars(self, var, val):

        print"2"
        vari[str(var)] = val

class maintest():

    def test(self):

        yes = raw_input("Yes: ")

        if yes == "1":
            yes = True
        else:
            yes = False

        self.result(yes)

    def result(self, reslt):

        if reslt == True:
            yes = True
        else:
            yes = False 

        Vars.SendVars('yes', yes)

a = maintest()
a.test()

print Vars.GetVars('yes')

Output:

Yes: 1
Traceback (most recent call last):
  File "/Users/jacobsifodaskalakis/Documents/LiClipse 
Workspace/Test/Test4.py", line 43, in <module>
    a.test()
  File "/Users/jacobsifodaskalakis/Documents/LiClipse 
Workspace/Test/Test4.py", line 31, in test
    self.result(yes)
  File "/Users/jacobsifodaskalakis/Documents/LiClipse 
Workspace/Test/Test4.py", line 40, in result
    Vars.SendVars('yes', yes)
TypeError: unbound method SendVars() must be called with Vars 
instance as first argument (got str instance instead)
Jake
  • 999
  • 1
  • 7
  • 16

1 Answers1

2

Looks like you need to instantiate your Vars class before you can use it:

def result(self, reslt):

    if reslt == True:
        yes = True
    else:
        yes = False 

    vars_instance = Vars()

    vars_instance.SendVars('yes', yes)

take a look at this question: How to instantiate a class in python

You also need to instantiate it near the end of your script:

vars_instance = Vars()

print vars_instance.GetVars('yes')

finally, you've forgotten to actually define your global vari

global vari
vari = {}
perfect5th
  • 1,992
  • 1
  • 17
  • 18
  • 1
    You are my friggin hero! This has been bothering me so much, and I knew it was something stupidly simple like that, I had the global defined but I somehow didn't paste it into the question lol. Again thank you!!! – Jake Jul 06 '17 at 00:01
  • No problem! As an aside, I would avoid naming any variables `vars`, as that has a special meaning in Python https://docs.python.org/2.7/library/functions.html#vars I did it in my answer for convenience. Think I'll change it now. – perfect5th Jul 06 '17 at 00:03
  • Yeah, Eclipse yelled at me when I put it in so I changed it to just var which seemed to work fine. – Jake Jul 06 '17 at 00:53