Skip to content Skip to sidebar Skip to footer

Wxpython Panel Is Cropped With Only A Small Box Shown At The Top Left Hand Corner

I am using Hide() and Show() from wx to do the 'next page' effect by hiding a panel and showing the next one but in the same frame (not very sure if I am doing it correctly though)

Solution 1:

@igor is correct a call to Layout will get the job done. Here is an example: Click on the displayed panel to swap to the other one.

import wx

classMyPanel(wx.Panel):
    def__init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.panel = wx.Panel(self)
        self.btn = wx.Button(self.panel, label="Panel 1", size=(250,75))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        self.panel.SetSizer(vbox1)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.panel)
        self.SetSizer(vbox)

        self.Show()

    defswitch(self, event):
        self.parent.Swap()

classMyOtherPanel(wx.Panel):
    def__init__(self, parent):
        super().__init__(parent)
        self.parent = parent

        self.panel = wx.Panel(self)
        self.btn = wx.Button(self.panel, label="Panel 2", size=(175,250))
        self.btn.Bind(wx.EVT_BUTTON, self.switch)

        vbox1 = wx.BoxSizer(wx.VERTICAL)
        vbox1.Add(self.btn)
        self.panel.SetSizer(vbox1)

        vbox = wx.BoxSizer(wx.VERTICAL)
        vbox.Add(self.panel)
        self.SetSizer(vbox)

        self.Show()
        self.panel.Hide()

    defswitch(self, event):
        self.parent.Swap()

classPanelSwitcher(wx.Frame):
    def__init__(self):
        super().__init__(None)

        vbox = wx.BoxSizer(wx.VERTICAL)
        self.panel1 = MyPanel(self)
        self.panel2 = MyOtherPanel(self)
        vbox.Add(self.panel1)
        vbox.Add(self.panel2)
        self.SetSizer(vbox)
        self.Show()

    defSwap(self):
        if self.panel1.panel.IsShown():
            self.panel1.panel.Hide()
            self.panel2.panel.Show()
        else:
            self.panel2.panel.Hide()
            self.panel1.panel.Show()
        self.Layout()


if __name__ == "__main__":
    app = wx.App()
    PanelSwitcher()
    app.MainLoop()

Solution 2:

I also had the problem a very long time and did not know the solution. The sizers did not work (as I expected)

For me, the problem was, that the panel had no (or the incorrect size). The solution was eiter:

panel.Fit() or panel.SetSize(x,y)

Another possibility was, to first add the panel into a sizer. And then set them to the frame. Afterwards put the buttons into the sizer - and add them to the panel. This also solves the incorrect size of the panel.

Post a Comment for "Wxpython Panel Is Cropped With Only A Small Box Shown At The Top Left Hand Corner"