在wxpython中对象的对齐

2024-10-03 21:33:21 发布

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

我给BoxSizer加了一些按钮。 这就是我想要的: enter image description here

这就是我要做的: enter image description here

这是第二个盒子的代码:

    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)    

我怎么修?在


Tags: selfrightaddbuttonitem按钮wxpanel
2条回答

有很多方法可以达到您想要的效果,包括使用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()

下面的代码将给出您想要的结果。在

enter image description here

但是请注意,对齐是通过设置比例和添加一个垫片来完成的,该垫片将按钮推到右侧。我发现这比摆弄对齐标志更容易理解/易读。在

    # Defining panel
    self.panel_1 = wx.Panel(self,-1)

    # create dummy button quick
    quickbtn = lambda label: wx.Button(self.panel_1, -1, label)

    stat1 = wx.StaticBox(self.panel_1, -1)
    stsz1 = wx.StaticBoxSizer(stat1, wx.HORIZONTAL)
    stsz1.Add((0,0), 1) # dummy spacer with proportion 1 filling the space on the left
    stsz1.Add(quickbtn('btn1'), 0, wx.EXPAND)

    stat2 = wx.StaticBox(self.panel_1, -1)
    stsz2 = wx.StaticBoxSizer(stat2, wx.HORIZONTAL)
    stsz2.Add((0,0), 1)
    stsz2.Add(quickbtn('btn2_A'), 0, wx.EXPAND)
    stsz2.Add(quickbtn('btn2_B'), 0, wx.EXPAND)

    szmain = wx.BoxSizer(wx.VERTICAL)
    szmain.Add(stsz1, 0, wx.EXPAND|wx.ALL, 2)
    szmain.Add(stsz2, 0, wx.EXPAND|wx.ALL, 2)

    self.panel_1.SetSizer(szmain)

相关问题 更多 >