New panel activating only when window resized: WxPython
I want to delete a panel ( mainPanel )and replace a panel ( secPanel )in its place. The code I have tried:
import wx
class secPanel ( wx.Panel ):
def __init__ ( self, parent ):
wx.Panel.__init__ ( self, parent = parent )
wx.TextCtrl ( self, pos = ( 100, 100 ) )
class mainPanel ( wx.Panel ):
def __init__ ( self, parent ):
wx.Panel.__init__ ( self, parent = parent )
self.parent = parent
b = wx.Button ( self, label = "Test", pos = ( 100, 100 ) )
self.Bind ( wx.EVT_BUTTON, self.onPress, b )
def onPress ( self, event ):
parent = self.parent
self.Destroy()
secPanel ( parent )
class Window ( wx.Frame ):
def __init__ ( self ):
wx.Frame.__init__ ( self, parent = None, title = "" )
mainPanel ( self )
self.Show()
if __name__ == "__main__":
app = wx.App()
window = Window()
app.MainLoop()
The mainPanel gets deleted but the secPanel only shows when the window is resized ( even just a pixel works ). Until I resize it shows a grey screen ( the one which would show without any panel ). This is quite annoying.
1 answer
-
answered 2022-01-19 19:26
Rolf of Saxony
In this case
Layout()
is your friend.
NormallyLayout()
is used when sizers are involved but in this case, there are no sizers but theparent
is awx.TopLevelWindow
.Layout
lays out the children using the window sizer or resizes the only child of the window to cover its entire area.wx.TopLevelWindow.Layout
overrides the base class Layout method to check if this window contains exactly one child – which is commonly the case, with wx.Panel being often created as the only child of wx.TopLevelWindow – and, if this is the case, resizes this child window to cover the entire client area.See
parent.Layout()
below:import wx class secPanel ( wx.Panel ): def __init__ ( self, parent ): wx.Panel.__init__ ( self, parent = parent ) wx.TextCtrl ( self, pos = ( 100, 100 ) ) class mainPanel ( wx.Panel ): def __init__ ( self, parent ): wx.Panel.__init__ ( self, parent = parent ) self.parent = parent b = wx.Button ( self, label = "Test", pos = ( 100, 100 ) ) self.Bind ( wx.EVT_BUTTON, self.onPress, b ) def onPress ( self, event ): parent = self.parent self.Destroy() secPanel ( parent ) parent.Layout() class Window ( wx.Frame ): def __init__ ( self ): wx.Frame.__init__ ( self, parent = None, title = "" ) mainPanel ( self ) self.Show() if __name__ == "__main__": app = wx.App() window = Window() app.MainLoop()
do you know?
how many words do you know