Python Unbound Method TypeError
方法
TypeError: unbound method get_pos() must be called with app instance as first argument (got nothing instead)
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | class app(object): def __init__(self,root): self.functionframe=FunctionFrame(root, self) self.functionframe.pack(side=BOTTOM) def get_pos(self): self.functionframe.input(self) class FunctionFrame(Frame): def __init__(self,master,parent): Frame.__init__(self,master,bg="grey90") self.entry = Entry(self,width=15) self.entry.pack def input(self): self.input = self.entry.get() return self.input |
您报告了此错误:
TypeError: unbound method get_pos() must be called with app instance as first argument (got nothing instead)
用外行的话来说,这意味着你在做这样的事情:
1 2 3 4 5 | class app(object): def get_pos(self): ... ... app.get_pos() |
你需要做的是这样的事情:
1 2 | the_app = app() # create instance of class 'app' the_app.get_pos() # call get_pos on the instance |
很难找到比这更具体的代码,因为您没有向我们显示导致错误的实际代码。
我在构建类实例时忘记在类名中添加括号时遇到了这个错误:
从my.package导入myclass
1 2 3 4 5 6 7 8 9 10 | # wrong instance = MyClass instance.someMethod() # tries to call MyClass.someMethod() # right instance = MyClass() instance.someMethod() |
我的水晶球告诉我,你用类
当然,正如其他人指出的那样,您发布的代码还有很多其他问题,很难猜测您没有发布的代码中的错误。