我对python很陌生,所以我尝试了python包和模块,但是我的项目中出现了一个错误
Menu.InitUI
TypeError: InitUI()缺少一个必需的位置参数:‘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()`发布于 2016-06-03 05:51:53
简单地说,def InitUI(self):和def OnQuit(self, e):是属于一个类的,而且在类中似乎没有它们。self引用函数所属类的当前实例。
发布于 2016-06-03 06:03:36
如果def InitUI()方法不属于任何“菜单”类,则不需要任何自参数。不需要执行Menu.InitUI(),因为您已经导入了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----https://stackoverflow.com/questions/37606279
复制相似问题