wxPython GUI BoxSizers

时间:2010-09-23 02:37:25

标签: python user-interface wxpython

好的我有一个我正在编码的应用程序,并且我试图让布局更简单: Sizer Alignment with all the input boxes aligned

注意文本左对齐和输入框是否对齐,我在wxPython演示代码中看到了这一点,但它们都使用了flexgrid sizer,我试图只使用BoxSizers(因为它们更简单,因为我只了解一些sizer,甚至在使用BoxSizers时也会遇到困难,在6个月内我会有更难的时间)

我尝试将输入和文本放在两个垂直大小调整器中,然后将它们放在水平大小调整器中,因为文本未与输入对齐而无效。我也试过这样做,并且还有每个文本,输入配对的sizer,甚至更糟。有什么建议吗?

2 个答案:

答案 0 :(得分:3)

以下是使用BoxSizers的简单示例:

import wx

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)

        # create the labels
        lblOne = wx.StaticText(panel, label="labelOne", size=(60,-1))
        lblTwo = wx.StaticText(panel, label="lblTwo", size=(60,-1))
        lblThree = wx.StaticText(panel, label="lblThree", size=(60,-1))

        # create the text controls
        txtOne = wx.TextCtrl(panel)
        txtTwo = wx.TextCtrl(panel)
        txtThree = wx.TextCtrl(panel)

        # create some sizers
        mainSizer = wx.BoxSizer(wx.VERTICAL)
        lineOneSizer = wx.BoxSizer(wx.HORIZONTAL)
        lineTwoSizer = wx.BoxSizer(wx.HORIZONTAL)
        lineThreeSizer = wx.BoxSizer(wx.HORIZONTAL)

        # add widgets to sizers
        lineOneSizer.Add(lblOne, 0, wx.ALL|wx.ALIGN_LEFT, 5)
        lineOneSizer.Add(txtOne, 0, wx.ALL, 5)
        lineTwoSizer.Add(lblTwo, 0, wx.ALL|wx.ALIGN_LEFT, 5)
        lineTwoSizer.Add(txtTwo, 0, wx.ALL, 5)
        lineThreeSizer.Add(lblThree, 0, wx.ALL|wx.ALIGN_LEFT, 5)
        lineThreeSizer.Add(txtThree, 0, wx.ALL, 5)

        mainSizer.Add(lineOneSizer)
        mainSizer.Add(lineTwoSizer)
        mainSizer.Add(lineThreeSizer)

        panel.SetSizer(mainSizer)

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

但这有点混乱,所以这里有一个重构版本:

import wx

class MyForm(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

        # create the main sizer
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)

        # Add a panel so it looks the correct on all platforms
        self.panel = wx.Panel(self, wx.ID_ANY)

        lbls = ["labelOne", "lblTwo", "lblThree"]
        for lbl in lbls:
            self.buildLayout(lbl)
        self.panel.SetSizer(self.mainSizer)

    #----------------------------------------------------------------------
    def buildLayout(self, text):
        """"""
        lblSize = (60,-1)
        lbl = wx.StaticText(self.panel, label=text, size=lblSize)
        txt = wx.TextCtrl(self.panel)

        sizer = wx.BoxSizer(wx.HORIZONTAL)
        sizer.Add(lbl, 0, wx.ALL|wx.ALIGN_LEFT, 5)
        sizer.Add(txt, 0, wx.ALL, 5)
        self.mainSizer.Add(sizer)

# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()

答案 1 :(得分:1)

对于除最基本以外的大多数布局,您通常无法使用多种不同类型的大小调整来逃避,以实现您的设计。

Here是一个关于sizer的好教程。