0

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

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()
4

1 回答 1

0

原来这只是一个小故障。在WindowsWhole_Sizer中归因于Main_Panel时,所有的小部件都位于左上角。一旦我开始通过拖动整个窗口的角来弄乱窗口大小,整个事情就自行解决了。现在我只觉得愚蠢。

于 2017-04-25T06:34:24.570 回答