gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 03:55:11 24 4
gpt4 key购买 nike

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

Menu.InitUI

类型错误: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()`

最佳答案

简短的回答是 def InitUI(self):def OnQuit(self, e): 属于一个类,而且您似乎不知道没有他们在一个类(class)里。 self 指的是函数所属类的当前实例。

关于python - 如何在Python中创建包和模块?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37606279/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com