wx.BoxSizer以VERTICAL和HORIZONTAL为中心

时间:2014-11-23 23:02:59

标签: python wxpython

我想将{strong>垂直和水平元素与wx.BoxSizer居中。我尝试了这个不成功(结果:元素垂直居中但不是水平居中......):

vsizer = wx.BoxSizer(wx.VERTICAL)
hsizer = wx.BoxSizer(wx.HORIZONTAL)
vsizer.AddStretchSpacer(1)
vsizer.Add(hsizer, 0, wx.ALL, 15)
vsizer.AddStretchSpacer(1)
self.SetSizer(vsizer)
hsizer.AddStretchSpacer(1)
hsizer.Add(wx.StaticBitmap(self, -1, myimg), 0, wx.ALL, 15)
hsizer.AddStretchSpacer(1) 

如何使用wx.BoxSizer垂直和水平居中元素?

1 个答案:

答案 0 :(得分:2)

这是一个简单的例子:

import wx

class Frame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "Vertical center")
        self.text = wx.StaticText(self, label=("This should be centered both"
                                   " vertically and horizontally"))
        sizer_v = wx.BoxSizer(wx.VERTICAL)
        sizer_h = wx.BoxSizer(wx.HORIZONTAL)
        sizer_h.Add(self.text, 1, wx.CENTER)
        sizer_v.Add(sizer_h, 1, wx.CENTER)
        self.SetSizer(sizer_v)

        self.Show()

app = wx.App()
Frame()
app.MainLoop()

wx.CENTERwx.BoxSizer内的元素居中(滚动到页面底部):http://wiki.wxpython.org/BoxSizerTutorial
HTH