简体   繁体   中英

wxPython middle align items in sizer

I am dynamically changing the number of wx objects in my GUI. The code is working but the alignment of the objects is not perfect. The static text objects are not middle aligned with the text objects.

错位

Notice that the word 'if' is not perfectly aligned with 'field name'.

Code:

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

Please could someone advise on how I can fix this alignment issue.

Thanks a lot in advance!

Duh! It turns out I just needed to change the alignment when adding items to the row sizer.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM