1

我是 python 新手,所以我尝试了 python 包和模块,但是我的项目中有一个错误,不知道这有什么问题。,

菜单.initUI

TypeError:InitUI() 缺少 1 个必需的位置参数:'self'

我有三个文件
1)__init__.py
2)Main.py
3)Menu.Py

     `<----------------__init__.py file------------>`
        from Main import main
        from Menu import InitUI

      <-------------------Menu.Py file------------>


    import wx

     def InitUI(self):

        menubar = wx.MenuBar()

        fileMenu = wx.Menu()
        fileMenu.Append(wx.ID_NEW, '&New')
        fileMenu.Append(wx.ID_OPEN, '&Open')
        fileMenu.Append(wx.ID_SAVE, '&Save')
        fileMenu.AppendSeparator()

        imp = wx.Menu()
        imp.Append(wx.ID_ANY,'Import File')

        fileMenu.AppendMenu(wx.ID_ANY,'I&mport',imp)

        qmi = wx.MenuItem(fileMenu,wx.ID_EXIT,'&Quit\tCtrl+Q')
        fileMenu.AppendItem(qmi)

        # EDIT Menu
         editMenu = wx.Menu()
         editMenu.Append(wx.ID_EDIT, '&Edit')


        #Help Menu
        helpMenu = wx.Menu()
        helpMenu.Append(wx.ID_HELP,'&Help')

        self.Bind(wx.EVT_MENU, self.OnQuit,qmi)

        menubar.Append(fileMenu, '&File')
        self.SetMenuBar(menubar)

        menubar.Append(editMenu, '&Edit')
        self.SetMenuBar(menubar)

        menubar.Append(helpMenu, '&Help')
        self.SetMenuBar(menubar)


        self.Centre()
        self.Show(True)


    def OnQuit(self,e):
        self.Close()

     <----------------Main.py--------------------->

         class Main_Frame(wx.Frame):
             def __init__(self,parent,title):
             super(Main_Frame,self).__init__(parent,title="Siemens MTBF",
                                                  size=   (1280,960)) 

         Menu.InitUI()       

         def main():
                ex = wx.App()
                Main_Frame(None,title='Center')
                ex.MainLoop()    


          if __name__ == '__main__':

           main()`
4

2 回答 2

1

简短的回答是,def InitUI(self):并且def OnQuit(self, e):被认为属于一个类,而且看起来你在一个类中没有它们。self指函数所属类的当前实例。

于 2016-06-03T05:51:53.097 回答
1

如果 def InitUI() 方法不属于任何“菜单”类,那么您不需要任何 self 参数。由于您已导入 InitUI() 方法,因此无需执行 Menu.InitUI()。因此,只需将其称为 InitUI()。正如您已将函数声明为 InitUI(self) 但调用为 Menu.InitUI() 这就是问题出现的原因,因为我们期望参数 self 的方法。从 InitUI() 中删除 self 并简单地调用没有“菜单”的 InitUI() 将解决您的问题。它就像:在 Menu.py

def InitUI():
    ---body---

在 Main.py 中:

----other peice of code----
InitUI()
----other peice of code----
于 2016-06-03T06:03:36.257 回答