wxPython 隐藏() 和显示() 改变面板对齐

2024-05-08 04:41:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在创建一个wxPython应用程序,它有一个管理部分,允许用户对用户登录数据库。我有一个面板、数据网格和一组按钮,这些按钮将根据单击的按钮而隐藏和显示。单击“添加”或“编辑”时self.userEntryPnl以及self.SaveBtn将显示和self.newGrid公司, 自身地址, 自我编辑,和自删除BTN会藏起来的。然后当self.SaveBtn被点击就会发生相反的情况。当我出现的时候self.userEntryPnl面板左对齐这里是我的代码:

在主.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()

在用户视图.py在

^{pr2}$

我怎样才能得到self.userEntryPnl以及self.SaveBtn在父面板的中心对齐?在

谢谢你的帮助!在


Tags: the用户selfid面板titlemain按钮
1条回答
网友
1楼 · 发布于 2024-05-08 04:41:22

我对用户视图.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按钮完全对齐到中心,您必须自己解决这个问题。我建议您将bottompanel添加到gridpnl和{}按钮添加到gridpnl和{}按钮,而不是{}。这样你只需要隐藏或显示面板。在

相关问题 更多 >