Skip to content Skip to sidebar Skip to footer

Wxpython Hide And Show Panel

I am creating a Python application that requires a login on start up. I want to have a main panel that will hold my login panel and on successful login the login panel will hide an

Solution 1:

Got it! I was trying to redraw the frame with Layout(), but I needed to redraw the BoxSizer with Layout() I added the following code to the login button:

defEvtLoginBtn(self,e):
    self.nb.Show()
    self.mainLogin.Hide()
    self.mainSizer.Layout()

Solution 2:

Call the Layout() method of the Frame and Panel like this

defEvtLoginBtn(self,e):
    self.nb.Show()
    self.mainLogin.Hide()
    self.Layout()
    self.nb.Layout()

The last call is probably not needed. Just check the program without it as well. The Layout() method redraws the Frame or Panel or whichever widget it is a method of.

UPDATE

Your code is obviously very big and has several other parts which I do not know. Anyways, here's something that I came up with, along the lines of the code you have provided, to show how you can do this. Hope this helps:

Main.py

import wx
from LoginPanel import LoginPanel

classMyFrame ( wx.Frame ):

    def__init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"Something something", pos = wx.DefaultPosition, size = wx.Size( 300,300 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

        self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

        BoxSizer0 = wx.BoxSizer( wx.VERTICAL )

        # Add login panel
        self.login_panel = LoginPanel(self)
        BoxSizer0.Add(self.login_panel, 1, wx.EXPAND | wx.ALL, 0)

        # Add notebook panel and its pages
        self.nb = wx.Notebook( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
        self.nb_subpanel1 = wx.Panel( self.nb, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.nb.AddPage( self.nb_subpanel1, u"something", False )
        self.nb_subpanel2 = wx.Panel( self.nb, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.TAB_TRAVERSAL )
        self.nb.AddPage( self.nb_subpanel2, u"something else", False )

        BoxSizer0.Add( self.nb, 1, wx.EXPAND |wx.ALL, 0 )

        # Adds a logout button
        self.logout_button = wx.Button( self, wx.ID_ANY, u"Logout", wx.DefaultPosition, wx.DefaultSize, 0 )
        BoxSizer0.Add( self.logout_button, 0, wx.ALIGN_CENTER|wx.ALL, 5 )

        # Hide nb and logout button
        self.nb.Hide()
        self.logout_button.Hide()


        self.SetSizer( BoxSizer0 )
        self.Layout()

        self.Centre( wx.BOTH )
        self.Show()

        # Connect Events
        self.logout_button.Bind( wx.EVT_BUTTON, self.on_logout )

    # Virtual event handlers, override them in your derived classdefon_logout( self, event ):
        self.nb.Hide()
        self.logout_button.Hide()
        self.login_panel.Show()
        self.Layout()

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

LoginPanel.py

import wx

classLoginPanel ( wx.Panel ):

    def__init__( self, parent ):
        wx.Panel.__init__ ( self, parent, id = wx.ID_ANY, pos = wx.DefaultPosition, size = wx.Size( 300,300 ), style = wx.TAB_TRAVERSAL )

        BoxSizer01 = wx.BoxSizer( wx.VERTICAL )

        self.login_button = wx.Button( self, wx.ID_ANY, u"Login", wx.DefaultPosition, wx.DefaultSize, 0 )
        self.login_button.SetDefault() 
        BoxSizer01.Add( self.login_button, 0, wx.ALIGN_CENTER|wx.ALL, 5 )


        self.SetSizer( BoxSizer01 )
        self.Layout()

        # Connect Events
        self.login_button.Bind( wx.EVT_BUTTON, self.on_login )


    # Virtual event handlers, overide them in your derived classdefon_login( self, event ):
        self.Hide()
        self.Parent.nb.Show()
        self.Parent.logout_button.Show()
        self.Parent.Layout()

Post a Comment for "Wxpython Hide And Show Panel"