Search code examples
pythonwxpythonalignment

wxPython Hide() and Show() Changes Panel Alignment


I am creating a wxPython application that has an administrative section that allows a user to users to the login database. I have a panel, data grid, and a set of buttons that will hide and show depending on which button is clicked. When add or edit are clicked the self.userEntryPnl and the self.SaveBtn will show and the self.newGrid, self.AddBtn, self.EditBtn, and self.DeleteBtn will hide. Then when the self.SaveBtn is clicked the opposite will happen. When I show self.userEntryPnl the panel is aligned to the left Here is my code:

main.py

import wx
from UserView import UserView

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title=title, size=(850,725))

        # Creating Panels
        self.main = wx.Panel(self)
        # Create a notebook on the panel
        self.nb = wx.Notebook(self.main, 1)

        # create the page windows as children of the notebook
        self.userAdmin = UserView(parent=self.nb, ID=-1)

        # add the pages to the notebook with the label to show on the tab
        self.nb.AddPage(self.userAdmin, "Users")

        # Create sizers
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        # Adding Objects to mainSizer
        self.mainSizer.AddSpacer(10)
        #self.mainSizer.Add(self.mainLogin, 1, wx.ALL|wx.EXPAND)
        self.mainSizer.Add(self.nb, 1, wx.ALL|wx.EXPAND)

        # Set main sizer
        self.main.SetAutoLayout(True)
        self.main.SetSizer(self.mainSizer)
        self.mainSizer.Fit(self.main)
        self.Layout()
        self.Centre(wx.BOTH)
        self.Show()

app = wx.App(False)
frame = MyFrame(None, -1, 'App UI')
app.MainLoop()

UserView.py

import wx

class UserView(wx.Panel):
        def __init__(self, parent, ID):
            wx.Panel.__init__(self, parent, ID)
            self.sizer = wx.BoxSizer(wx.VERTICAL)

            self.gridPnl = wx.Panel(self,-1)
            self.gridPnlSizer = wx.BoxSizer(wx.HORIZONTAL)

            #Grid that show current users
            self.newGrid = wx.grid.Grid(self.gridPnl)
            self.newGrid.CreateGrid(6,3)
            self.newGrid.SetColLabelValue(0, "ID")
            self.newGrid.SetColLabelValue(1, "Username")
            self.newGrid.SetColLabelValue(2, "Password")

            #Start out showing this Grid until the Add button or Edit button is clicked
            self.newGrid.Show()


            self.userEntryPnl = wx.Panel(self.gridPnl,-1)
            self.userEntryPnlSizer = wx.BoxSizer(wx.VERTICAL)

            self.userlbl = wx.StaticText(self.userEntryPnl, label='Username')
            self.userTxt = wx.TextCtrl(self.userEntryPnl, size=(140,-1))
            self.userTxt.SetModified(True)
            self.userTxt.SetEditable(True)
            self.passlbl = wx.StaticText(self.userEntryPnl, label='Password')
            self.passTxt = wx.TextCtrl(self.userEntryPnl, size=(140,-1))
            self.passTxt.SetModified(True)
            self.passTxt.SetEditable(True)

            self.userEntryPnlSizer.AddStretchSpacer()
            self.userEntryPnlSizer.Add(self.userlbl,0,wx.EXPAND)
            self.userEntryPnlSizer.AddSpacer(10)
            self.userEntryPnlSizer.Add(self.userTxt,0,wx.EXPAND)
            self.userEntryPnlSizer.AddSpacer(10)
            self.userEntryPnlSizer.Add(self.passlbl,0,wx.EXPAND)
            self.userEntryPnlSizer.AddSpacer(10)
            self.userEntryPnlSizer.Add(self.passTxt,0,wx.EXPAND)
            self.userEntryPnlSizer.AddStretchSpacer()

            self.userEntryPnl.SetAutoLayout(True)
            self.userEntryPnl.SetSizer(self.userEntryPnlSizer)
            self.userEntryPnlSizer.Fit(self.userEntryPnl)

            #start out hiding this panel until Add button or Edit button is clicked
            self.userEntryPnl.Hide()

            self.gridPnlSizer.AddStretchSpacer()
            self.gridPnlSizer.Add(self.newGrid,0,wx.EXPAND)
            self.gridPnlSizer.Add(self.userEntryPnl,0,wx.EXPAND)
            self.gridPnlSizer.AddStretchSpacer()

            self.gridPnl.SetAutoLayout(True)
            self.gridPnl.SetSizer(self.gridPnlSizer)
            self.gridPnlSizer.Fit(self.gridPnl)


            self.bottomBtnsPnl = wx.Panel(self,-1)
            self.bottomSizer = wx.BoxSizer(wx.HORIZONTAL)

            self.AddBtn = wx.Button(self.bottomBtnsPnl, label='Add')
            self.EditBtn = wx.Button(self.bottomBtnsPnl, label='Edit')
            self.DeleteBtn = wx.Button(self.bottomBtnsPnl, label='Delete')
            self.SaveBtn = wx.Button(self.bottomBtnsPnl, label='Save')

            self.AddBtn.Show()
            self.EditBtn.Show()
            self.DeleteBtn.Show()
            self.SaveBtn.Hide()

            self.Bind(wx.EVT_BUTTON, self.addBtnEnt, self.AddBtn)
            self.Bind(wx.EVT_BUTTON, self.editBtnEnt, self.EditBtn)
            self.Bind(wx.EVT_BUTTON, self.deleteBtnEnt, self.DeleteBtn)
            self.Bind(wx.EVT_BUTTON, self.saveBtnEnt, self.SaveBtn)


            self.bottomSizer.AddStretchSpacer()
            self.bottomSizer.Add(self.AddBtn,0,wx.EXPAND)
            self.bottomSizer.AddSpacer(10)
            self.bottomSizer.Add(self.EditBtn,0,wx.EXPAND)
            self.bottomSizer.AddSpacer(10)
            self.bottomSizer.Add(self.DeleteBtn,0,wx.EXPAND)
            self.bottomSizer.AddSpacer(10)
            self.bottomSizer.Add(self.SaveBtn,0,wx.EXPAND)
            self.bottomSizer.AddSpacer(10)
            self.bottomSizer.AddStretchSpacer()

            self.bottomBtnsPnl.SetAutoLayout(True)
            self.bottomBtnsPnl.SetSizer(self.bottomSizer)
            self.bottomSizer.Fit(self.bottomBtnsPnl)

            self.sizer.AddSpacer(10)
            self.sizer.Add(self.gridPnl,0,wx.EXPAND)
            self.sizer.AddSpacer(10)
            self.sizer.Add(self.bottomBtnsPnl,0,wx.EXPAND)
            self.sizer.AddSpacer(10)

            self.SetAutoLayout(True)
            self.SetSizer(self.sizer)
            self.sizer.Fit(self)

        def addBtnEnt(self,e):
            self.newGrid.Hide()
            self.AddBtn.Hide()
            self.EditBtn.Hide()
            self.DeleteBtn.Hide()
            self.SaveBtn.Show()
            self.userEntryPnl.Show()
            self.userEntryPnl.Layout()

        def editBtnEnt(self,e):
            self.newGrid.Hide()
            self.AddBtn.Hide()
            self.EditBtn.Hide()
            self.DeleteBtn.Hide()
            self.SaveBtn.Show()
            self.userEntryPnl.Show()
            self.userEntryPnl.Layout()

        def deleteBtnEnt(self,e):
            print("Delete clicked")

        def saveBtnEnt(self,e):
            self.userEntryPnl.Hide()
            self.SaveBtn.Hide()
            self.newGrid.Show()
            self.AddBtn.Show()
            self.EditBtn.Show()
            self.DeleteBtn.Show()
            self.userEntryPnl.Layout()

How do I get the self.userEntryPnl and the self.SaveBtn to align in the center of the parent panel?

Thank you for any and all help!


Solution

  • I made some changes to the UserView.py, now it will show the widgets aligned to the center of the frame.

    Here's the code

    import wx.grid
    
    class UserView(wx.Panel):
            def __init__(self, parent, ID):
                wx.Panel.__init__(self, parent, ID)
                self.sizer = wx.BoxSizer(wx.VERTICAL)
    
                self.gridPnl = wx.Panel(self,-1)
                self.gridPnlSizer = wx.BoxSizer(wx.HORIZONTAL)
    
                #Grid that show current users
                self.newGrid = wx.grid.Grid(self.gridPnl)
                self.newGrid.CreateGrid(6,3)
                self.newGrid.SetColLabelValue(0, "ID")
                self.newGrid.SetColLabelValue(1, "Username")
                self.newGrid.SetColLabelValue(2, "Password")
    
                #Start out showing this Grid until the Add button or Edit button is clicked
                self.newGrid.Show()
    
    
                self.userEntryPnl = wx.Panel(self,-1)
                self.userEntryPnlSizer = wx.BoxSizer(wx.VERTICAL)
    
                self.userlbl = wx.StaticText(self.userEntryPnl, label='Username')
                self.userTxt = wx.TextCtrl(self.userEntryPnl, size=(140,-1))
                self.userTxt.SetModified(True)
                self.userTxt.SetEditable(True)
                self.passlbl = wx.StaticText(self.userEntryPnl, label='Password')
                self.passTxt = wx.TextCtrl(self.userEntryPnl, size=(140,-1))
                self.passTxt.SetModified(True)
                self.passTxt.SetEditable(True)
    
                self.userEntryPnlSizer.AddStretchSpacer()
                self.userEntryPnlSizer.Add(self.userlbl,0,wx.ALL|wx.ALIGN_CENTER)
                self.userEntryPnlSizer.AddSpacer(10)
                self.userEntryPnlSizer.Add(self.userTxt,0,wx.ALL|wx.ALIGN_CENTER)
                self.userEntryPnlSizer.AddSpacer(10)
                self.userEntryPnlSizer.Add(self.passlbl,0,wx.ALL|wx.ALIGN_CENTER)
                self.userEntryPnlSizer.AddSpacer(10)
                self.userEntryPnlSizer.Add(self.passTxt,0,wx.ALL|wx.ALIGN_CENTER)
                self.userEntryPnlSizer.AddStretchSpacer()
    
                self.userEntryPnl.SetAutoLayout(True)
                self.userEntryPnl.SetSizer(self.userEntryPnlSizer)
                self.userEntryPnlSizer.Fit(self.userEntryPnl)
    
                #start out hiding this panel until Add button or Edit button is clicked
                self.userEntryPnl.Hide()
    
                self.gridPnlSizer.AddStretchSpacer()
                self.gridPnlSizer.Add(self.newGrid,0,wx.EXPAND)
                self.gridPnlSizer.Add(self.userEntryPnl,0,wx.EXPAND)
                self.gridPnlSizer.AddStretchSpacer()
    
                self.gridPnl.SetAutoLayout(True)
                self.gridPnl.SetSizer(self.gridPnlSizer)
                self.gridPnlSizer.Fit(self.gridPnl)
    
    
                self.bottomBtnsPnl = wx.Panel(self,-1)
                self.bottomSizer = wx.BoxSizer(wx.HORIZONTAL)
    
                self.AddBtn = wx.Button(self.bottomBtnsPnl, label='Add')
                self.EditBtn = wx.Button(self.bottomBtnsPnl, label='Edit')
                self.DeleteBtn = wx.Button(self.bottomBtnsPnl, label='Delete')
                self.SaveBtn = wx.Button(self.bottomBtnsPnl, label='Save')
    
                self.AddBtn.Show()
                self.EditBtn.Show()
                self.DeleteBtn.Show()
                self.SaveBtn.Hide()
    
                self.Bind(wx.EVT_BUTTON, self.addBtnEnt, self.AddBtn)
                self.Bind(wx.EVT_BUTTON, self.editBtnEnt, self.EditBtn)
                self.Bind(wx.EVT_BUTTON, self.deleteBtnEnt, self.DeleteBtn)
                self.Bind(wx.EVT_BUTTON, self.saveBtnEnt, self.SaveBtn)
    
    
                self.bottomSizer.AddStretchSpacer()
                self.bottomSizer.Add(self.AddBtn,0,wx.EXPAND)
                self.bottomSizer.AddSpacer(10)
                self.bottomSizer.Add(self.EditBtn,0,wx.EXPAND)
                self.bottomSizer.AddSpacer(10)
                self.bottomSizer.Add(self.DeleteBtn,0,wx.EXPAND)
                self.bottomSizer.AddSpacer(10)
                self.bottomSizer.Add(self.SaveBtn,0,wx.ALL|wx.ALIGN_CENTER)
                self.bottomSizer.AddSpacer(10)
                self.bottomSizer.AddStretchSpacer()
    
                self.bottomBtnsPnl.SetAutoLayout(True)
                self.bottomBtnsPnl.SetSizer(self.bottomSizer)
                self.bottomSizer.Fit(self.bottomBtnsPnl)
    
                self.sizer.AddSpacer(10)
                self.sizer.Add(self.gridPnl,0,wx.EXPAND)
                self.sizer.AddSpacer(10)
                self.sizer.Add(self.userEntryPnl,0,wx.EXPAND)
                self.sizer.AddSpacer(10)
                self.sizer.Add(self.bottomBtnsPnl,0,wx.EXPAND)
                self.sizer.AddSpacer(10)
    
                self.SetAutoLayout(True)
                self.SetSizer(self.sizer)
                self.sizer.Fit(self)
    
            def addBtnEnt(self,e):
                self.gridPnl.Hide()
                self.AddBtn.Hide()
                self.EditBtn.Hide()
                self.DeleteBtn.Hide()
                self.SaveBtn.Show()
                self.userEntryPnl.Show()
                self.userEntryPnl.Layout()
                self.bottomBtnsPnl.Layout()
                self.Layout()
    
            def editBtnEnt(self,e):
                self.gridPnl.Hide()
                self.AddBtn.Hide()
                self.EditBtn.Hide()
                self.DeleteBtn.Hide()
                self.SaveBtn.Show()
                self.userEntryPnl.Show()
                self.userEntryPnl.Layout()
                self.Layout()
    
            def deleteBtnEnt(self,e):
                print("Delete clicked")
    
            def saveBtnEnt(self,e):
                self.userEntryPnl.Hide()
                self.SaveBtn.Hide()
                self.gridPnl.Show()
                self.AddBtn.Show()
                self.EditBtn.Show()
                self.DeleteBtn.Show()
                self.gridPnl.Layout()
                self.Layout()
    

    This doesn't completely align the Save button to the center, you'll have to figure that out for yourself. I would suggest that instead of a bottompanel you add the add, edit and delete buttons to gridpnl and save button to usrviewpnl. This way you only need to hide or show the panels.