如何防止wx面板在其他尺寸调整器上重叠

时间:2019-05-24 12:16:15

标签: python python-3.x wxpython

基本上,我试图完成一个简单的隐藏面板并在单击按钮时显示另一个,如果框架中只有一个缩放器,我通常能够做到这些,但是在这种情况下,我有一个色带,所以我有2个缩放器。框架,每当我在sizer 2中显示一个面板时,它就会重叠并显示出来,好像它不属于sizer。

我起草了一个基本的彩色示例来重现该问题,请注意,单击按钮时,蓝色面板将隐藏黄色面板,而不是替换灰色面板。

import wx


class MyForm(wx.Frame):

    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "HomeFinance", size = (1400,700))
        self.panel1 = RibbonPanel(self)
        self.panel2 = SidePanel(self)
        self.panel3 = MainPanel(self)
        self.panel4 = NewPanel(self)

        self.panel1.SetBackgroundColour(wx.Colour(191, 193, 65))
        self.panel2.SetBackgroundColour(wx.Colour(56, 122, 45))
        self.panel3.SetBackgroundColour(wx.Colour(77, 84, 96, 0))
        self.panel4.SetBackgroundColour(wx.Colour(55, 149, 153, 0))
        self.panel4.Hide()

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.hsizer = wx.BoxSizer(wx.HORIZONTAL)

        self.sizer.Add(self.panel1, 1, wx.EXPAND)
        self.hsizer.Add(self.panel4, 1, wx.EXPAND)
        self.hsizer.Add(self.panel3, 1, wx.EXPAND)
        self.hsizer.Add(self.panel2, 1, wx.EXPAND)
        self.sizer.Add(self.hsizer, 1, wx.EXPAND)

        self.Bind(wx.EVT_BUTTON, self.onetimer, self.panel2.m_button1)
        self.SetSizer(self.sizer)
        self.CenterOnScreen()
        self.Show()

    def onetimer(self, event):
        self.panel3.Hide()
        self.panel4.Show()


class RibbonPanel(wx.Panel):
    def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(1400, 115), style=wx.TAB_TRAVERSAL,
                 name=wx.EmptyString):
        wx.Panel.__init__(self, parent, id=id, pos=pos, size=size, style=style, name=name)
        self.Layout()

class MainPanel(wx.Panel):
    def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(1000, 545), style=wx.TAB_TRAVERSAL,
                 name=wx.EmptyString):
        wx.Panel.__init__(self, parent, id=id, pos=pos, size=size, style=style, name=name)
        self.Layout()


class SidePanel(wx.Panel):
    def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(400, 545), style=wx.TAB_TRAVERSAL,
                 name=wx.EmptyString):
        wx.Panel.__init__(self, parent, id=id, pos=pos, size=size, style=style, name=name)
        self.m_button1 = wx.Button(self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
        self.Layout()


class NewPanel(wx.Panel):
    def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition, size=wx.Size(1000, 545), style=wx.TAB_TRAVERSAL,
                 name=wx.EmptyString):
        wx.Panel.__init__(self, parent, id=id, pos=pos, size=size, style=style, name=name)
        self.Layout()

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

蓝色面板有望替换灰色面板,并且具有完全相同的位置。

1 个答案:

答案 0 :(得分:1)

更改显示的小部件时,需要调整大小器来计算新的位置和大小。因此,您的代码仅缺少对self.Layout()

中对onetimer的调用

方法应为:

def onetimer(self, event):
    self.panel3.Hide()
    self.panel4.Show()
    self.Layout()