Skip to main content
 首页 » 编程设计

python之wxpython中的对象对齐

2025年05月04日14Terrylee

我向 BoxSizer 添加了一些按钮。 这是我想要的:

这就是我要去的地方:

这是第二个盒子的代码:

    self.approveItem = wx.Button(self.panel_1, -1, "Approve Item") 
    self.changeQty = wx.Button(self.panel_1, -1, "Change Qty") 
    sizer_2 = wx.BoxSizer(wx.HORIZONTAL) 
    sizer_2.Add(self.approveItem, 0, wx.ALIGN_RIGHT | wx.RIGHT, 0) 
    sizer_2.Add(self.changeQty, 0, wx.ALIGN_RIGHT| wx.RIGHT, 0) 
    self.sizer_item_staticbox = wx.StaticBox(self, -1, "") 
    sizer_item = wx.StaticBoxSizer(self.sizer_item_staticbox, wx.HORIZONTAL) 
    sizer_item.SetMinSize((600,-1)) 
    sizer_item.Add(sizer_2, 0, wx.EXPAND, 0)     

我该如何解决?

请您参考如下方法:

有很多方法可以实现您想要的效果,包括使用 flexgridsizer、gridsizer 和普通的水平和垂直 boxsizer,这里是一个 gridsizer 示例。
我将其设置为 5 列宽,以便您可以根据需要在左侧插入额外的按钮。
请参阅:http://wxpython.org/Phoenix/docs/html/GridSizer.html

#!/usr/bin/env python 
import wx 
class MyFrame(wx.Frame): 
    def __init__(self, parent, ID, title): 
        wx.Frame.__init__(self, parent, ID, title, wx.DefaultPosition) 
        Buttons = [] 
        Buttons.append(wx.Button(self,-1, "Approve Location")) 
        Buttons.append(wx.Button(self,-1, "Approve Item")) 
        Buttons.append(wx.Button(self,-1, "Change Qty")) 
        Buttons.append(wx.Button(self,-1, "Approve")) 
        sizer = wx.GridBagSizer(5,3) 
        sizer.Add(Buttons[0], (0, 5), (1,1), wx.EXPAND) 
        sizer.Add(Buttons[1], (1, 4), (1,1), wx.EXPAND) 
        sizer.Add(Buttons[2], (1, 5), (1,1), wx.EXPAND) 
        sizer.Add(Buttons[3], (2, 5), (1,1), wx.EXPAND) 
        self.SetSizerAndFit(sizer) 
        self.Centre() 
class MyApp(wx.App): 
    def OnInit(self): 
        frame = MyFrame(None, -1, "Gridbagsizer") 
        frame.Show(True) 
        self.SetTopWindow(frame) 
        return True 
if __name__ == "__main__": 
    app = MyApp(0) 
    app.MainLoop()