我的问题与这个问题基本相同:
Why Does .Hide()ing and .Show()ing Panels in wxPython Result in the Sizer Changing the Layout?
未显示最终代码段,我不明白他们如何应用列出的解决方案。我没有问题切换面板,只是保持布局。我下面有一个功能测试脚本。
import wx
import wx.lib.sized_controls as sized_controls
class BluePanel(sized_controls.SizedScrolledPanel):
def __init__(self, parent):
super(BluePanel, self).__init__(parent, wx.ID_ANY, name="BluePanel")
bmp = wx.ArtProvider.GetBitmap(wx.ART_INFORMATION, wx.ART_OTHER, (12, 12))
titleIco = wx.StaticBitmap(self, wx.ID_ANY, bmp)
title = wx.StaticText(self, wx.ID_ANY, 'My Blue Panel Title With Many Words')
title.SetFont(wx.Font(20, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD, faceName='Inconsolata'))
title.SetForegroundColour("blue")
bmp = wx.ArtProvider.GetBitmap(wx.ART_TIP, wx.ART_OTHER, (12, 12))
inputOneIco = wx.StaticBitmap(self, wx.ID_ANY, bmp)
labelOne = wx.StaticText(self, wx.ID_ANY, 'Input 1')
inputTxtOne = wx.TextCtrl(self, wx.ID_ANY, '')
self.titleSizer = wx.BoxSizer(wx.HORIZONTAL)
inputOneSizer = wx.BoxSizer(wx.HORIZONTAL)
self.titleSizer.Add(titleIco, 0, wx.ALL, 5)
self.titleSizer.Add(title, 0, wx.ALL, 5)
inputOneSizer.Add(inputOneIco, 0, wx.ALL, 5)
inputOneSizer.Add(labelOne, 0, wx.ALL, 5)
inputOneSizer.Add(inputTxtOne, 1, wx.ALL|wx.EXPAND, 5)
self.panel_sizer = wx.BoxSizer(wx.VERTICAL)
self.panel_sizer.Add(self.titleSizer, 0, wx.CENTER)
self.panel_sizer.Add(wx.StaticLine(self, ), 0, wx.ALL | wx.EXPAND, 5)
self.panel_sizer.Add(inputOneSizer, 0, wx.ALL | wx.EXPAND, 5)
self.panel_sizer.Add(wx.StaticLine(self), wx.EXPAND)
self.SetSizerAndFit(self.panel_sizer)
# self.SetSizer(self.panel_sizer)
class WelcomePanel(wx.Panel):
def __init__(self, parent):
super(WelcomePanel, self).__init__(parent, wx.ID_ANY, name="WelcomePanel")
title = wx.StaticText(self, wx.ID_ANY, 'My Welcome Title With Many Words')
title.SetFont(wx.Font(20, wx.FONTFAMILY_DEFAULT, wx.NORMAL, wx.BOLD, faceName='Inconsolata'))
title.SetForegroundColour("red")
self.title_sizer = wx.BoxSizer(wx.HORIZONTAL)
self.panel_sizer = wx.BoxSizer(wx.VERTICAL)
self.title_sizer.Add(title, 0, wx.ALL, 5)
self.panel_sizer.Add(self.title_sizer, 0, wx.CENTER, 5)
self.SetSizer(self.panel_sizer)
class MainFrame(wx.Frame):
def __init__(self):
# Setup a frame that is centered on the screen and opens at 75% of full screen.
window_name = "My GUI"
display_x, display_y = wx.DisplaySize()
scale = 0.75
super(MainFrame, self).__init__(None, wx.ID_ANY, title=window_name, size=(display_x*scale, display_y*scale))
self.Centre()
self.build_menu_bar()
self.main_sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self.main_sizer)
self.blue_panel = BluePanel(self)
self.blue_panel.Hide()
self.welcome_panel = WelcomePanel(self)
# self.welcome_panel.Hide()
self.panel_dict = \
{"BluePanel": self.blue_panel, "WelcomePanel": self.welcome_panel}
self.panel = self.welcome_panel
self.main_sizer.Add(self.panel, 0, wx.ALL | wx.EXPAND, 5)
self.Layout()
def switch_panels(self, event, switch_id):
self.panel.Hide()
self.panel = self.panel_dict[switch_id]
self.panel.Show()
# self.panel.Layout()
# self.panel.GetParent().GetSizer().Show(self.panel)
# self.panel.GetParent().GetSizer().Fit(self.panel)
# self.panel.GetParent().GetSizer().Layout()
self.Layout()
def build_menu_bar(self):
menu_bar = wx.MenuBar()
# Build the "File" menu.
file_menu = wx.Menu()
welcome_app = file_menu.Append(wx.ID_ANY, "Home", "Go to Welcome Screen")
file_menu.AppendSeparator()
exit_app = file_menu.Append(wx.ID_EXIT, "E&xit\tAlt-x", "Close window and exit program.")
menu_bar.Append(file_menu, "&File")
# Build the "Tools" Menu
tools_menu = wx.Menu()
blue_app = tools_menu.Append(wx.ID_ANY, "Blue")
menu_bar.Append(tools_menu, "&Tools")
self.SetMenuBar(menu_bar)
# Bind Menu Items to Actions
self.Bind(wx.EVT_MENU, self.on_exit, exit_app)
# Panel switching
self.Bind(wx.EVT_MENU, lambda event, temp=blue_app:
self.switch_panels(event, self.blue_panel.GetName()), blue_app)
self.Bind(wx.EVT_MENU, lambda event, temp=welcome_app:
self.switch_panels(event, self.welcome_panel.GetName()), welcome_app)
def on_exit(self, event):
"""Close the frame, terminating the application."""
self.Close(True)
def main():
app = wx.App()
frame = MainFrame()
frame.Show()
app.MainLoop()
if __name__ == '__main__':
main()
我知道我可能在这里遗漏了一些简单的东西,但是我无法解决这个问题。任何帮助将不胜感激。谢谢。