Search code examples
pythonwxpython

Switch to another panel wxpython


I'm learning wxpython.I read the documentation and after playing little bit with it, now I'm creating a small application containing some panels. On One panel I have created a login page. OnSubmit of the login panel, I want to switch to another panel. I'm not sure how to do that. Here is my code (ScreenGrab might help you) ScreenGrab might help you : (Unwanted function and class definitions not shown here):

Toolbook_Demo.py

class ToolbookDemo( wx.Toolbook ) :

  def __init__( self, parent ) :
    print ""
    wx.Toolbook.__init__( self, parent, wx.ID_ANY, style=
                          wx.BK_DEFAULT
                          #wx.BK_TOP
                          #wx.BK_BOTTOM
                          #wx.BK_LEFT
                          #wx.BK_RIGHT
                         )
    # Make an image list using the LBXX images
    il = wx.ImageList( 32, 32 )
    for x in range( 4 ) :
        imgObj = getattr( images, 'LB%02d' % ( x+1 ) )
        bmp = imgObj.GetBitmap()
        il.Add( bmp )

    self.AssignImageList( il )
    imageIdGenerator = getNextImageID( il.GetImageCount() )
    panellogin = userlogin.TabPanel( self )
    print panellogin.Hide() 
    notebookPageList = [ (userlogin.TabPanel( self ),  'Login'),
                         (panelTwo.TabPanel( self ),   'Panel Two'),
                         (panelThree.TabPanel( self ), 'Panel Three'),
                         (panelOne.TabPanel( self ),   'Home')]
    imID = 0
    for page, label in notebookPageList :
        self.AddPage( page, label, imageId=imageIdGenerator.next() )
        imID += 1

    # An undocumented method in the official docs :
    self.ChangeSelection( 0 )   # Select and view this notebook page.
                                # Creates no events - method SetSelection does.

    self.Bind( wx.EVT_TOOLBOOK_PAGE_CHANGING, self.OnPageChanging )
    self.Bind( wx.EVT_TOOLBOOK_PAGE_CHANGED,  self.OnPageChanged )

userlogin.py

 import wx
 class TabPanel( wx.Panel ) :
   """ This will be [inserted into] the first notebook tab. """

   def __init__( self, parent ) :
       wx.Panel.__init__( self, parent=parent, id=wx.ID_ANY )
       sizer = wx.FlexGridSizer(rows=3, cols=2, hgap=5, vgap=15)

    # Username
    self.txt_Username = wx.TextCtrl(self, 1, size=(150, -1))
    lbl_Username = wx.StaticText(self, -1, "Username:")
    sizer.Add(lbl_Username,0, wx.LEFT|wx.TOP| wx.RIGHT, 50)
    sizer.Add(self.txt_Username,0, wx.TOP| wx.RIGHT, 50)


    # Password
    self.txt_Password = wx.TextCtrl(self, 1, size=(150, -1), style=wx.TE_PASSWORD)
    lbl_Password = wx.StaticText(self, -1, "Password:")
    sizer.Add(lbl_Password,0, wx.LEFT|wx.RIGHT, 50)
    sizer.Add(self.txt_Password,0, wx.RIGHT, 50)

    # Submit button
    btn_Process = wx.Button(self, -1, "&Login")
    self.Bind(wx.EVT_BUTTON, self.OnSubmit, btn_Process)
    sizer.Add(btn_Process,0, wx.LEFT, 50)

    self.SetSizer(sizer)

def OnSubmit(self, event):
    UserText = self.txt_Username.GetValue()
    PasswordText = self.txt_Password.GetValue()
    if authentcated(UserText, PasswordText):
         #Switch to another panel 
         #Hide login panel until current session expires
         #Show another panels only

Solution

  • I don't think you want to login using a panel in a Notebook-type widget that will just take you to the next tab. The user can click on the next tab without logging in. I am guessing you would like the first tab's panel to change to something else. The easiest way to do that is to call it's Hide method and Show a different panel. Fortunately, that's really easy to do. Here's an example that uses a menu to switch between the panels:

    import wx
    import wx.grid as gridlib
    
    ########################################################################
    class PanelOne(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent=parent)
            txt = wx.TextCtrl(self)
    
    ########################################################################
    class PanelTwo(wx.Panel):
        """"""
    
        #----------------------------------------------------------------------
        def __init__(self, parent):
            """Constructor"""
            wx.Panel.__init__(self, parent=parent)
    
            grid = gridlib.Grid(self)
            grid.CreateGrid(25,12)
    
            sizer = wx.BoxSizer(wx.VERTICAL)
            sizer.Add(grid, 0, wx.EXPAND)
            self.SetSizer(sizer)
    
    ########################################################################
    class MyForm(wx.Frame):
    
        #----------------------------------------------------------------------
        def __init__(self):
            wx.Frame.__init__(self, None, wx.ID_ANY, 
                              "Panel Switcher Tutorial")
    
            self.panel_one = PanelOne(self)
            self.panel_two = PanelTwo(self)
            self.panel_two.Hide()
    
            self.sizer = wx.BoxSizer(wx.VERTICAL)
            self.sizer.Add(self.panel_one, 1, wx.EXPAND)
            self.sizer.Add(self.panel_two, 1, wx.EXPAND)
            self.SetSizer(self.sizer)
    
    
            menubar = wx.MenuBar()
            fileMenu = wx.Menu()
            switch_panels_menu_item = fileMenu.Append(wx.ID_ANY, 
                                                      "Switch Panels", 
                                                      "Some text")
            self.Bind(wx.EVT_MENU, self.onSwitchPanels, 
                      switch_panels_menu_item)
            menubar.Append(fileMenu, '&File')
            self.SetMenuBar(menubar)
    
        #----------------------------------------------------------------------
        def onSwitchPanels(self, event):
            """"""
            if self.panel_one.IsShown():
                self.SetTitle("Panel Two Showing")
                self.panel_one.Hide()
                self.panel_two.Show()
            else:
                self.SetTitle("Panel One Showing")
                self.panel_one.Show()
                self.panel_two.Hide()
            self.Layout()
    
    
    # Run the program
    if __name__ == "__main__":
        app = wx.App(False)
        frame = MyForm()
        frame.Show()
        app.MainLoop()
    

    You can use the same logic to swap out a panel in the Toolbook.