gpt4 book ai didi

python - wxPython 在 sizer 中居中对齐项目

转载 作者:太空宇宙 更新时间:2023-11-03 21:39:33 26 4
gpt4 key购买 nike

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

misalignment

请注意,“if”一词与“字段名称”并不完全对齐。

代码:

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

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

提前非常感谢!

最佳答案

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

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

关于python - wxPython 在 sizer 中居中对齐项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52983145/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com