首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在python中创建包和模块?

如何在python中创建包和模块?
EN

Stack Overflow用户
提问于 2016-06-03 05:01:34
回答 2查看 97关注 0票数 1

我对python很陌生,所以我尝试了python包和模块,但是我的项目中出现了一个错误

Menu.InitUI

TypeError: InitUI()缺少一个必需的位置参数:‘self’

我有三个文件

1)__init__.py

2)Main.py

3)Menu.Py

代码语言:javascript
复制
     `<----------------__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()`
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-03 05:51:53

简单地说,def InitUI(self):def OnQuit(self, e):是属于一个类的,而且在类中似乎没有它们。self引用函数所属类的当前实例。

票数 1
EN

Stack Overflow用户

发布于 2016-06-03 06:03:36

如果def InitUI()方法不属于任何“菜单”类,则不需要任何自参数。不需要执行Menu.InitUI(),因为您已经导入了InitUI()方法。因此,只需像InitUI()那样调用它。由于您已经将函数声明为InitUI( self ),但调用为Menu.InitUI(),这就是为什么问题作为方法出现的原因,我们期待参数self。从InitUI()中删除self,只需调用InitUI()而不使用“菜单”就可以解决问题。类似于:在Menu.py中

代码语言:javascript
复制
def InitUI():
    ---body---

在Main.py中:

代码语言:javascript
复制
----other peice of code----
InitUI()
----other peice of code----
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37606279

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档