如何在wxpython中绘制面板的边框中心?

时间:2018-04-26 12:01:21

标签: python-2.7 wxpython

我尝试使用pos和size将边框中心放在面板上,但边框保持不变。它没有移动到中心并尝试使用 wx.ALIGN_CENTER

以下是我的代码:

import wx    
class myframe(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title= "asdsad")
        self.login_panel = PasswordPanel(self)         
        self.Layout()
        self.ShowFullScreen(True)
        self.Show()

class PasswordPanel(wx.Panel):    
      def __init__(self, parent):
           wx.Panel.__init__(self, parent=parent)
           self.panel = wx.Panel(self,pos=(350,350), size=(500,250), style=wx.SUNKEN_BORDER)    
if __name__ == '__main__':

    app = wx.App(False)
    frame = myframe()
    app.MainLoop()

上面的代码按预期工作,但如果我在面板代码中包含以下内容,边框将会扩展。

self.quote = wx.StaticText(self.panel, label="")
           self.result = wx.StaticText(self.panel, label="")
           self.result.SetForegroundColour(wx.RED)
           self.ok_button = wx.Button(self.panel, label="Ok")
           self.cancel_button = wx.Button(self.panel, wx.ID_OK, 'Cancel')
           self.lblname = wx.StaticText(self.panel, label="Enter Password:")
           self.editname = wx.TextCtrl(self.panel, size=(240, -1), style=wx.TE_PASSWORD)
           self.ok_button.SetFocus()

           # Set sizer for the panel content
           self.sizer = wx.GridBagSizer(0, 0)
           self.sizer.Add(self.quote, (15, 45), flag = wx.ALL, border = 5)
           self.sizer.Add(self.result, (15, 46), flag = wx.ALL, border = 5)
           self.sizer.Add(self.lblname, (16, 45), flag = wx.ALL, border = 5)
           self.sizer.Add(self.editname, pos = (16,46), span = (1,3), flag = wx.EXPAND|wx.ALL, border = 5)
           self.sizer.Add(self.ok_button, pos = (17, 47),flag = wx.ALL, border = 5)
           self.sizer.Add(self.cancel_button, pos = (17, 48),flag = wx.ALL, border = 5)
           self.windowSizer = wx.BoxSizer()
           self.windowSizer.Add(self.panel, 1, wx.ALL | wx.EXPAND, 5) 
           # Use the sizers 
           self.panel.SetSizerAndFit(self.sizer)  
           self.SetSizerAndFit(self.windowSizer)  
       self.Layout()

请就此提出建议。

1 个答案:

答案 0 :(得分:0)

当您将面板放入BoxSizer并self.SetSizerandFit时,您将面板扩展为填充PasswordPanel的父selfmyframe)的大小

如果删除下面注释的代码,边框将保持在面板的中心位置:

    # Set sizer for the panel content
    self.sizer = wx.GridBagSizer(0, 0)
    self.sizer.Add(self.quote, (15, 45), flag = wx.ALL, border = 5)
    self.sizer.Add(self.result, (15, 46), flag = wx.ALL, border = 5)
    self.sizer.Add(self.lblname, (16, 45), flag = wx.ALL, border = 5)
    self.sizer.Add(self.editname, pos = (16,46), span = (1,3), flag = wx.EXPAND|wx.ALL, border = 5)
    self.sizer.Add(self.ok_button, pos = (17, 47),flag = wx.ALL, border = 5)
    self.sizer.Add(self.cancel_button, pos = (17, 48),flag = wx.ALL, border = 5)
    #self.windowSizer = wx.BoxSizer()
    #self.windowSizer.Add(self.panel, 0, wx.ALL, 5)
    # Use the sizers
    self.panel.SetSizerAndFit(self.sizer)
    #self.SetSizerAndFit(self.windowSizer)
    self.Layout()