Skip to content Skip to sidebar Skip to footer

WxPython Panel In Existing Window: Slow And Small

I'm experiencing very different behavior when creating a wx.Panel depending on whether the main window's already called Show(). With the below code, the application opens quickly w

Solution 1:

The panel will get the correct size if the Frame feels a SizeEvent. So this works for your second question:

def OnNew(self, evt):
    if self.panel:
        self.panel.Destroy()
    self.panel = MainPanel(self)
    self.SendSizeEvent()

Control of windows and widgets becomes easier by using sizers. With sizers in your main frame you can use the sizer Layout method to fit widgets in place.

Could not find a way of speeding up panel rewrite. But you can diminish the bizarre visual effect by not deleting the panel but clearing the sizer instead (you dont need SendSizeEvent() for this case):

class MainPanel(wx.lib.scrolledpanel.ScrolledPanel):
    def __init__(self,parent):
        wx.lib.scrolledpanel.ScrolledPanel.__init__(self, parent=parent)
        self.SetupScrolling()
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.fill()
        self.SetSizer(self.sizer)

    def fill(self):
        tup = [wx.StaticText(self, wx.ID_ANY, "I'm static text") for i in range(200)]
        self.sizer.AddMany(tup)
        self.Layout()

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="FrameTest", size=(600,800))
        self.InitMenu()
        self.panel = None
        self.panel = MainPanel(self)

    def InitMenu(self):
        self.menuBar = wx.MenuBar()
        menuFile = wx.Menu()
        menuFile.Append(wx.ID_NEW, "&New")
        self.Bind(wx.EVT_MENU, self.OnNew, id=wx.ID_NEW)
        self.menuBar.Append(menuFile, "&File")
        self.SetMenuBar(self.menuBar)

    def OnNew(self, evt):
        if self.panel:
            self.panel.sizer.Clear()
        self.panel.fill()

Post a Comment for "WxPython Panel In Existing Window: Slow And Small"