1

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()`
3
  • 1
    is there a class Menu(...): somewhere above def InitUI(self):? Commented Jun 3, 2016 at 5:06
  • No there is no class menu only the def InitUI(self) method in the Menu.py.Is it needed to create a class in the Menu.py Commented Jun 3, 2016 at 5:50
  • Hi @mukeshkrishnan if the def InitUI() method is not of any "Menu" class then you dont need any self parameter in that. No need to do Menu.InitUI() since you have imported the InitUI() method. So simply call it like InitUI(). As you have declared the function as InitUI(self) but calling as Menu.InitUI() thats why the problem is coming as the method us expecting a paramater self. Commented Jun 3, 2016 at 5:57

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Can u able to share the code..which your are suggesting the modification
That's impossible to do without seeing the rest of the code, how it's structured, and what calls what.
@ Will ,I put all my code on the question itself.Only three files i had FYI,Thanks in advance
Then this is hard to answer--it looks like you copied and pasted part of it, because without a class, self makes no sense at all.
No i didn't copied and paste it,What i did i code a wxpython frame with menu.Later i decided to make it as structure so i followed some tutorial and change my code and make it as three file.
1

if the def InitUI() method is not of any "Menu" class then you dont need any self parameter in that. No need to do Menu.InitUI() since you have imported the InitUI() method. So simply call it like InitUI(). As you have declared the function as InitUI(self) but calling as Menu.InitUI() thats why the problem is coming as the method us expecting a paramater self. Remove the self from InitUI() and simply call InitUI() without "Menu" will resolve your issue. Its like: In Menu.py

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

In Main.py:

----other peice of code----
InitUI()
----other peice of code----

3 Comments

But if i took self from it,it show me an error Undefined menubar(34th line) similarly i got error on the line:37,40,43...So share me the full code.Thanks in advance
@mukeshkrishnan place the InitiUI(self) method inside the class that has SetMenuBar(). See self is used for the class member functions. So create the object of that class which contain InitiUI(self) method then using that object call InitiUI() method.
Menu.InitUI() wont work. Create object of Menu. Using that object call InitUI. Ex: Menu obj. obj.InitUI()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.