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()
做self.Main_Panel.SetSizer(Whole_Sizer)'在Xubuntu 16.04中適用於wxPython 3.0.2.0和Python 2.7。不知道爲什麼這不適合你。我會說'self.SetSizer(Whole_Sizer)'只是讓我的機器上的窗口完全空了。 –
我在Windows 7和10中使用Python 2.7來試用它。特別設置sizer爲self.Main_Panel會導致所有小部件合併到左上角。 –
在Linux mint 18中嘗試過,它也可以在那裏使用。也許這是我認爲這是Windows窗體的問題。任何想法如何去解決它? –