【发布时间】:2014-05-01 08:56:53
【问题描述】:
我有一个单独的主应用文件 (f.py) 和一个单独的面板文件 (p.py)。我从Panel 中将Menubar 设置为Frame。这是我的工作代码:
f.py
import wx
import p
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1, "Checking Menubar from external Panel")
mainPanel = wx.Panel(self, -1)
mainPanel.SetBackgroundColour(wx.Color(0, 255, 0))
sizer = wx.BoxSizer(wx.VERTICAL)
mainPanel.SetSizer(sizer)
subPanel = p.MyPanel(parent=mainPanel)
sizer.Add(subPanel, 1, wx.EXPAND)
# subPanel.createMenuBar()
self.Show()
if __name__ == '__main__':
app = wx.PySimpleApp()
frame = MyFrame()
app.MainLoop()
p.py
import wx
class MyPanel(wx.Panel):
def __init__(self, parent=None):
wx.Panel.__init__(self, parent=parent, id=-1)
self.SetBackgroundColour(wx.Color(255, 0, 0))
self.createMenuBar()
def createMenuBar(self):
self.menuBar = wx.MenuBar()
self.mnuFile = wx.Menu()
self.mnuFile.Append(id=1, text="&New")
self.mnuFile.Append(id=1, text="&Open")
self.mnuFile.Append(id=1, text="E&xit")
self.menuBar.Append(menu=self.mnuFile, title="&File")
self.GetTopLevelParent().SetMenuBar(self.menuBar)
以上代码将Menubar 添加到Frame。但是Panel 在输出窗口上没有得到全尺寸。这是输出图像:
现在,如果我从Frame 中调用Panel 的createMenu 函数(通过取消注释f.py 中的subPanel.createMenuBar() 行并评论self.createMenuBar() 中的self.createMenuBar() 行)它会根据需要提供输出。这是Panel 覆盖完整Frame 的输出图像:
我无法理解为什么在Panel 中设置Menubar 会扰乱它的大小。任何帮助表示赞赏。
【问题讨论】:
标签: wxpython panel frame menubar