问题描述
我有一个程序太长了,所以我把它分成单独的 .py 文件(这是我第一次这样做,所以我有点猜测最好的处理方式它)。
占用大量空间的一件事是为程序中的每一帧设置菜单栏。为了解决这个问题,我创建了一个单独的文件 modMenuBar.py
,其中包含我的菜单栏,它是 wx.MenuBar
的子类。它看起来像这样:
class modMenuBar(wx.MenuBar):
def __init__(self,parent,type):
super(modMenuBar,self).__init__()
self.fileMenu = wx.Menu()
if type == 'start':
self._startmenu()
elif type == 'restart':
self._resMenu()
else: None
self.fileMenu.AppendSeparator()
self.exitItem = self.fileMenu.Append(wx.ID_EXIT)
self.helpMenu = wx.Menu()
self.aboutItem = self.helpMenu.Append(wx.ID_ABOUT)
self.Append(self.fileMenu,"&File")
self.Append(self.helpMenu,"&Help")
def _startmenu(self):
self.openItem = self.fileMenu.Append(
-1,"&Open...\tCtrl-O","Open .csv file to analyze")
def _resMenu(self):
self.restartItem = self.fileMenu.Append(
-1,"&Restart...\tCtrl-R","Restart app from start window")
def _OnAbout(self,event):
"""display 'about' dialog."""
wx.MessageBox(f"DCB LLoD Calculator version {ver}\n\nDeveloped by:\n\n"
"[my name,workplace,and contact info redacted]","About",wx.OK | wx.ICON_informatION)
然后,在主程序中创建的每个框架中,我使用 __init__
在框架的 self.makeMenuBar()
中创建菜单栏。该方法在每个类中定义如下(注意: ldf
是包含子类菜单栏和其他一些杂项功能的单独文件的别名 ):
def makeMenuBar(self):
menuBar = ldf.modMenuBar('restart') # 'restart' Could be 'start' depending on the frame
self.SetMenuBar(menuBar)
self.Bind(wx.EVT_MENU,self._OnRestart,menuBar.restartItem)
self.Bind(wx.EVT_MENU,self._OnExit,menuBar.exitItem)
self.Bind(wx.EVT_MENU,menuBar._OnAbout,menuBar.aboutItem)
我想知道是否有办法在 self.SetMenuBar(menuBar)
中包含 self.Bind(...)
和 ldf.modMenuBar()
命令,所以在主文件中每个帧的 __init__
中,我只是必须调用 ldf.modMenuBar(*args)
并且它会自行完成所有绑定和设置,而不必在每个帧中仍然定义 makeMenuBar()
方法?
我认为我的问题归结为从子类 wx.MenuBar
对象修改框架的问题,但也许我在那里走错了路......有人知道如何做到这一点吗?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)