所以我试图创建一个在框架之间共享信息的GUI(设置为类)并遇到这个错误,尽管谷歌搜索和尝试不同的东西,但没有运气解决。我只是想创建一个可以写入其他类并从中读取的库。我创建了这个测试代码只是为了调试问题,所以如果你想查看我的主代码让我知道,谢谢。
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')
输出:
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)
答案 0 :(得分:2)
看起来你需要先实例化你的Vars类才能使用它:
def result(self, reslt):
if reslt == True:
yes = True
else:
yes = False
vars_instance = Vars()
vars_instance.SendVars('yes', yes)
看看这个问题:How to instantiate a class in python
您还需要在脚本末尾附近实例化它:
vars_instance = Vars()
print vars_instance.GetVars('yes')
最后,您忘记了实际定义global vari
global vari
vari = {}