【问题标题】:wxPython middle align items in sizerwxPython中间对齐sizer中的项目
【发布时间】:2018-10-25 06:56:04
【问题描述】:

我正在动态更改 GUI 中 wx 对象的数量。代码正在运行,但对象的对齐方式并不完美。静态文本对象未与文本对象居中对齐。

请注意,“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

请有人建议我如何解决此对齐问题。

提前非常感谢!

【问题讨论】:

    标签: python alignment wxpython


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-01
      • 2015-10-25
      相关资源
      最近更新 更多