正确的右对齐方法:
panel = wx.Panel(self)
button = wx.Button(panel, label = _(u'确定'), size = (60, 28))
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(button, 0, wx.ALL | wx.ALIGN_RIGHT, 10)
panel.SetSizer(hbox)
panel.SetAutoLayout(True)
wx中的Sizer的add讲解
Add(self,item, proportion=0,flag=0, border=0, userData=None)
item,与userData参数就不必讲解了,主要是proportion,flag,border三者的配合.
当flag有wx.EXPAND值时, proportion取0表示横向扩展,大于零表示同时横竖扩展,小于零应该只扩展竖方向。
当然flag中 存在wx.TOP,wx.BOTTOM,wx.LEFT,wx.RIGHT,wx.ALL中值时,表示与其它控件保持多少像素的距离,border参数指定
最后flag中的wx.ALIGN_* 开头的值就表示对齐方向,
例如如果想某控件一直右对齐,就在此控件左边放一个空sizer,如: box.Add((1,1), 0, wx.EXPAND,0 ), 其中必需有expand,第二个参数取0
下面是没找到官方正确布局的异端方法,大家学。
wx 布局真蛋痛,还是我没找到方法使控件右对齐. 最后只能自绘,绑定EVT_SZIE事件
#coding:utf-8
import wx
class ButtonBarPanel(wx.Panel):
def __init__(self, parent, pos):
super(ButtonBarPanel, self).__init__(parent = parent, id = -1, pos = pos, size = (510, 32), style = wx.NO_BORDER)
tsize = (24,24)
bmpBack = wx.ArtProvider.GetBitmap(wx.ART_NEW, wx.ART_TOOLBAR, tsize)
bmpForward = wx.ArtProvider.GetBitmap(wx.ART_FILE_OPEN, wx.ART_TOOLBAR, tsize)
self._btBack = wx.BitmapButton(self, 10, bmpBack, (0, 0),
(bmpBack.GetWidth() + 8, bmpBack.GetHeight() + 8), wx.NO_BORDER)
self._btBack.SetToolTipString("This is a bitmap button.")
self.Bind(wx.EVT_BUTTON, self.OnClick, self._btBack)
self._btGo = wx.BitmapButton(self, 20, bmpForward, (32, 0),
(bmpForward.GetWidth() + 8, bmpForward.GetHeight() + 8), wx.NO_BORDER)
self._btGo.SetToolTipString("open file.")
self.Bind(wx.EVT_BUTTON, self.OnClick, self._btGo)
self._btNew = self.addBtn(30, u'新建文件夹', (64, 0))
self._btOpen = self.addBtn(40, u'打开', (152, 0))
self._btShare = self.addBtn(50, u'共享', (240, 0))
#TODO:....ADD btn
self._ctrlSearch = wx.SearchCtrl(self, 80)
self.Bind(wx.EVT_SIZE, self.OnReSize)
def OnReSize(self, event):
#在绑定的size事件中使search右对齐
x, y = self.GetSize()
w, h = self._ctrlSearch.GetSize()
self._ctrlSearch.SetPosition((x - w - 8, 4))
self.Refresh()
def addBtn(self, id, lable, pos = (0, 0), tooltip = ''):
bt = wx.Button(self, id, lable, pos, size = (88, 30), style = wx.NO_BORDER)
if tooltip:
bt.SetToolTipString(tooltip)
else:
bt.SetToolTipString(lable)
self.Bind(wx.EVT_BUTTON, self.OnClick, bt)
return bt
def OnClick(self, event):
print "tool %s clicked\n" % event.GetId()
class F(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size = (500, 250))
menubar = wx.MenuBar()
file = wx.Menu()
file.Append(1, '& Quit', 'Exit Calculator')
menubar.Append(file, ' & File')
self.SetMenuBar(menubar)
ButtonBarPanel(self, (20, 20))
self.Bind(wx.EVT_MENU, self.OnClose, id = 1)
self.Show(True)
def OnClose(self, event):
self.Close()
app = wx.App()
F(None, -1, 'F')
app.MainLoop()
wx要布局,一定要在panel上放控件,frame上最少要有一个panel