wxPython在sizer中对齐项目

时间:2018-10-25 06:56:04

标签: python alignment wxpython

我正在GUI中动态更改wx对象的数量。该代码可以正常工作,但是对象的对齐方式并不完美。静态文本对象与文本对象不在中间对齐。

misalignment

请注意,“ if”一词与“ field name”没有完全对齐。

代码:

import wx


########################################################################
class MyPanel(wx.Panel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        wx.Panel.__init__(self, parent)
        self.numRows = 0
        self.frame = parent

        #create sizers
        self.mainSizer = wx.BoxSizer(wx.VERTICAL)
        controlSizer = wx.BoxSizer(wx.HORIZONTAL)
        self.colSizer = wx.BoxSizer(wx.VERTICAL)

        self.addButton = wx.Button(self, label="Add")
        self.addButton.Bind(wx.EVT_BUTTON, self.onAddWidget)
        controlSizer.Add(self.addButton, 0, wx.CENTER|wx.ALL, 5)

        self.removeButton = wx.Button(self, label="Remove")
        self.removeButton.Bind(wx.EVT_BUTTON, self.onRemoveWidget)
        controlSizer.Add(self.removeButton, 0, wx.CENTER|wx.ALL, 5)

        self.mainSizer.Add(controlSizer, 0, wx.CENTER)
        self.mainSizer.Add(self.colSizer, 0, wx.CENTER|wx.ALL, 10)

        self.SetSizer(self.mainSizer)

    #----------------------------------------------------------------------
    def onAddWidget(self, event):
        """"""
        self.numRows += 1

        #create the objects
        rowSizer =wx.BoxSizer(wx.HORIZONTAL)
        ifText = wx.StaticText(self, -1, 'If')
        fieldBox1 = wx.TextCtrl(self, -1, 'Field Name')
        dropDown1 = wx.Choice(self, -1, choices=['<', '<=', '=', '>=', '>'])
        fieldBox2 = wx.TextCtrl(self, -1, 'Field Name')
        thenText = wx.StaticText(self, -1, 'Then')
        fieldBox3 = wx.TextCtrl(self, -1, 'Field Name')


        #create a list of all the objects
        objects = [ifText, fieldBox1, dropDown1, fieldBox2, thenText, fieldBox3]

        #add object to row sizer
        for myObject in objects:
            rowSizer.Add(myObject, 0, wx.ALL, 5)    

        #add the objects to the sizer
        self.colSizer.Add(rowSizer, 0, wx.ALL, 5)

        self.frame.fSizer.Layout()
        self.frame.Fit()

    #----------------------------------------------------------------------
    def onRemoveWidget(self, event):
        """"""
        if self.colSizer.GetChildren():
            self.colSizer.Hide(self.numRows-1)
            self.colSizer.Remove(self.numRows-1)
            self.numRows -= 1
            self.frame.fSizer.Layout()
            self.frame.Fit()

########################################################################
class MyFrame(wx.Frame):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Frame.__init__(self, parent=None, title="Add / Remove Buttons")
        self.fSizer = wx.BoxSizer(wx.VERTICAL)
        panel = MyPanel(self)
        self.fSizer.Add(panel, 1, wx.EXPAND)
        self.SetSizer(self.fSizer)
        self.Fit()
        self.Show()

#----------------------------------------------------------------------
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyFrame()
    app.MainLoop()

del app

请有人提供有关如何解决此对齐问题的建议。

非常感谢!

1 个答案:

答案 0 :(得分:0)

Du!事实证明,在将项目添加到行大小调整器时,我只需要更改对齐方式。

rowSizer.Add(myObject, 0, wx.ALIGN_CENTER, 5)