Search code examples
pythonpython-3.xwxpythonpython-modulepython-packaging

How to create a package and modules in python?


I am new to python so I trying python packages and modules,but I had a error in my project don't know what was wrong in this.,

Menu.InitUI

TypeError: InitUI() missing 1 required positional argument:'self'

I had a three files
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()`

Solution

  • The short answer is that def InitUI(self): and def OnQuit(self, e): are ment to belong to a class, and it appears you don't have them in a class. self refers to the current instance of the class a function belongs to.