python 未绑定方法 <myMethod> 必须使用 <MyFrame> 实例作为第一个参数调用(得到的是空)

0 投票
3 回答
6634 浏览
提问于 2025-04-16 15:07

我在编译的时候遇到了这个错误:

C:\Temp\pythonWork\superGui>superGui.py
  Traceback (most recent call last):
  File "C:\Temp\pythonWork\superGui\superGui.py", line 747, in <module>
  MyFrame.disableAll()
  TypeError: unbound method disableAll() must be called with MyFrame instance as first argument (got nothing instead)

这是代码片段:

class MyFrame(wx.Frame):
    ---- CODE SNIP ----
    def disableAll(self):
        self.btnBeginInstall.Disable()
        self.btnBeginInstall.Disable()
        self.btnInfraSystem.Disable()
        self.btnIwpcSystem.Disable()
        self.btnIwpcSystem.Disable()
        self.btnIwpcIwpcdba.Disable()
        self.btnLdapOc4jadmin.Disable()
        self.btnLdapOrcladmin.Disable()
        self.btnIas_admin.Disable()
        self.btniwpcadmin.Disable()
        self.btnAll.Disable()

if __name__ == "__main__":
    app = wx.PySimpleApp(0)
    wx.InitAllImageHandlers()
    mainFrame = MyFrame(None, -1, "")
    app.SetTopWindow(mainFrame)
    mainFrame.Show()

    #disable the buttons
    success = MyFrame.disableAll()
    app.MainLoop()

我看了几个类似的问题,但答案并没有让我一下子明白,可能更可能的是我根本就不懂。

3 个回答

0

你在调用这个方法的时候,好像它是一个类方法或者静态方法。那self应该从哪里来呢?你需要在一个对象上调用这个方法,这个对象就是要执行某些操作的,比如说mainFrame.disableAll()

另外,success会是None,因为disableAll这个方法没有返回任何东西。你到底想要做什么呢?

3
mainFrame.disableAll()

可以用这个代替

MyFrame.disableAll()

这样做就可以了。

4

你可能需要使用那个类的一个实例。

mainFrame.disableAll()

这样做应该可以解决问题,就像其他回答里提到的那样。

撰写回答