使用哪种sizer以及如何实现? (wxPython凤凰城)

时间:2017-04-02 23:19:09

标签: wxpython

我需要在面板上放置5个小部件。

我想要的布局是:

StaticText:    TextCtrl
StaticText:    TextCtrl
         Button

我需要整个实施。您可以获得最干净的解决方案。

然而,我的尝试有点骇人听闻。

sizer1 = wx.BoxSizer(orient=wx.HORIZONTAL)
sizer2 = wx.BoxSizer(orient=wx.HORIZONTAL)
sizer3 = wx.BoxSizer(orient=wx.VERTICAL)

sizer1.Add(window=labelOfTextFieldForURL,
           border=5,
           flag=wx.ALL | wx.ALIGN_CENTER)
sizer1.Add(window=self.textFieldForURL,
           border=5,
           flag=wx.ALL | wx.ALIGN_CENTER)

sizer2.Add(window=labelOfTextFieldForSearchTerm,
           border=5,
           flag=wx.ALL | wx.ALIGN_CENTER)
sizer2.Add(window=self.textFieldForSearchTerm,
           border=5,
           flag=wx.ALL | wx.ALIGN_CENTER)

sizer3.AddStretchSpacer()
sizer3.Add(sizer=sizer1,
           border=5,
           flag=wx.ALL | wx.ALIGN_CENTER)
sizer3.Add(sizer=sizer2,
           border=5,
           flag=wx.ALL | wx.ALIGN_CENTER)
sizer3.Add(window=buttonCount,
           border=5,
           flag=wx.ALL | wx.ALIGN_CENTER)
sizer3.AddStretchSpacer()

panel.SetSizer(sizer3)
panel.Layout()

1 个答案:

答案 0 :(得分:0)

这是一个简单的应用程序,可以快速概述如何实现您的布局。有关说明,请参阅代码注释

import wx

class TestFrame(wx.Frame):
    def __init__(self, parent=None):
        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                          size=wx.Size(500, 300), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

        # create a flex grid sizer with 3 rows and 2 columns, 0 gaps between rows and columns
        flex_grid_sizer = wx.FlexGridSizer(3, 2, 0, 0)
        # make the TextCtrls expand
        flex_grid_sizer.AddGrowableCol(1)
        flex_grid_sizer.SetFlexibleDirection(wx.HORIZONTAL)
        flex_grid_sizer.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_ALL)

        # Start Row 1
        self.static_text_1 = wx.StaticText(self, wx.ID_ANY, "Static Text", wx.DefaultPosition, wx.DefaultSize, 0)
        self.static_text_1.Wrap(-1)
        flex_grid_sizer.Add(self.static_text_1, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL, 5)

        self.textctrl_1 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        flex_grid_sizer.Add(self.textctrl_1, 1,
                            wx.ALL | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, 5)

        # Start Row 2
        self.stat_text_2 = wx.StaticText(self, wx.ID_ANY, "Static Text", wx.DefaultPosition, wx.DefaultSize, 0)
        self.stat_text_2.Wrap(-1)
        flex_grid_sizer.Add(self.stat_text_2, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL, 5)

        self.textctrl_2 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        flex_grid_sizer.Add(self.textctrl_2, 1,
                            wx.ALL | wx.ALIGN_CENTER_HORIZONTAL | wx.ALIGN_CENTER_VERTICAL | wx.EXPAND, 5)

        # Start Row 3
        # Add a spacer to center the bytton
        flex_grid_sizer.AddStretchSpacer()

        self.button = wx.Button(self, wx.ID_ANY, "A Button", wx.DefaultPosition, wx.DefaultSize, 0)
        flex_grid_sizer.Add(self.button, 0, wx.ALL | wx.ALIGN_CENTER_VERTICAL | wx.ALIGN_CENTER_HORIZONTAL, 5)

        self.SetSizer(flex_grid_sizer)
        self.Layout()

        self.Centre(wx.BOTH)


app = wx.App()
frame = TestFrame()
frame.Show()
app.MainLoop()