wxPython Hide()和Show()更改面板对齐方式

时间:2014-08-22 21:21:53

标签: python wxpython alignment

我正在创建一个wxPython应用程序,它有一个管理部分,允许用户访问登录数据库。我有一个面板,数据网格和一组按钮,根据点击的按钮隐藏和显示。单击添加或编辑时,将显示self.userEntryPnl和self.SaveBtn,并且self.newGrid,self.AddBtn,self.EditBtn和self.DeleteBtn将隐藏。然后,当单击self.SaveBtn时,将发生相反的情况。当我显示self.userEntryPnl时,面板与左边对齐这是我的代码:

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()

如何让self.userEntryPnl和self.SaveBtn在父面板的中心对齐?

感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我对UserView.py进行了一些更改,现在它将显示与框架中心对齐的小部件。

这是代码

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()

这并没有将Save按钮与中心完全对齐,您必须自己解决这个问题。我建议您将bottompaneladdedit按钮添加到deletegridpnl按钮,而不是save usrviewpnl }}。这样您只需要隐藏或显示面板。