面板大小不会在带菜单的框架中展开

时间:2017-04-23 20:06:06

标签: python-2.7 wxpython sizer

我在wxpython中创建了一个带有菜单栏的框架,并将主sizer设置为框架本身的子框架,但在调整框架大小后框架的面板保持固定大小。当移除菜单栏并将主分级器设置到面板本身时,调整大小完美,但是当菜单添加到框架设置时,面板的主分级器不起作用。

tl; dr - 此代码的面板大小保持不变。如何使其可调整大小?

import wx

class Mainframe(wx.Frame):
def __init__(self,parent,title):
    wx.Frame.__init__(self,parent,title=title,size=(650,450))
    self.Main_Panel = wx.Panel(self,id=wx.ID_ANY)

    #################################################################
    ### Template menubar menubar
    #################################################################
    Menubar = wx.MenuBar()
    Filemenu = wx.Menu()
    Debugmenu = wx.Menu()

    self.Menu_Open_File = Filemenu.Append(wx.ID_OPEN,"Open","Open an nrix file to edit")
    self.Menu_SaveAs_File = Filemenu.Append(wx.ID_SAVEAS,"Save as","Normal saving will be incorporated later")
    Filemenu.AppendSeparator()
    self.Menu_About_Dialog = Filemenu.Append(wx.ID_ABOUT,"About","Information about the program and changelog")
    self.Debug_State_Switch = Debugmenu.Append(wx.ID_ANY,"Debug on","Turn on debug mode",wx.ITEM_CHECK)

    Menubar.Append(Filemenu,"File")
    Menubar.Append(Debugmenu,"Debug")
    self.SetMenuBar(Menubar)
    #################################################################
    ### Frame objects
    #################################################################
    Left_Static_text = wx.StaticText(self.Main_Panel,wx.ID_ANY,style=wx.ALIGN_LEFT)
    Left_Static_text.SetFont(wx.Font(16,wx.ROMAN,wx.NORMAL,wx.FONTWEIGHT_NORMAL))
    Left_Static_text.SetLabel("Exploitable text")

    Left_Text_Control = wx.TextCtrl(self.Main_Panel,wx.ID_ANY,value="Exploitable")

    Buttons = []
    for i in range(4):
        Buttons.append(wx.Button(self.Main_Panel,label=str(i + 1),size=(50,50)))
    #################################################################
    ### Frame sizers
    #################################################################
    Grid_Sizer = wx.GridSizer(2,2,15,15)
    Ver_Wrapper = wx.BoxSizer(wx.VERTICAL)
    Left_Sizer = wx.BoxSizer(wx.VERTICAL)
    Whole_Sizer = wx.BoxSizer(wx.HORIZONTAL)
    #################################################################
    ### Attaching sizers
    #################################################################
    for i in range(4):
        Grid_Sizer.Add(Buttons[i],0,wx.EXPAND)

    Ver_Wrapper.Add((0,0),proportion=1,flag=wx.EXPAND)
    Ver_Wrapper.Add(Grid_Sizer,proportion=0,flag=wx.CENTER)
    Ver_Wrapper.Add((0,0),proportion=1,flag=wx.EXPAND)

    Left_Sizer.Add((0,0),proportion=1,flag=wx.EXPAND)
    Left_Sizer.Add(Left_Static_text,proportion=0,flag=wx.CENTER)
    Left_Sizer.Add(Left_Text_Control,proportion=0,flag=wx.CENTER)
    Left_Sizer.Add((0,0),proportion=1,flag=wx.EXPAND)

    Whole_Sizer.Add(Left_Sizer,proportion=1,flag=wx.EXPAND)
    Whole_Sizer.Add(Ver_Wrapper,proportion=3,flag=wx.EXPAND)

    # self.Main_Panel.SetSizer(Whole_Sizer) <- THIS WORKS WITHOUT A MENU
    self.SetSizer(Whole_Sizer)

app = wx.App(False)
frame = Mainframe(None,"Menu UI")
frame.Show(True)
app.MainLoop()

1 个答案:

答案 0 :(得分:0)

原来这只是一个小问题。在Whole_Sizer归因于Windows中的Main_Panel时,所有小部件都位于左上角。一旦我开始通过拖动窗口角落弄乱窗口大小,整个事情就解决了。现在我感觉很蠢。