gpt4 book ai didi

python - wxPython GUI BoxSizers

转载 作者:太空宇宙 更新时间:2023-11-04 11:02:58 24 4
gpt4 key购买 nike

好的,我有一个正在编码的应用程序,我正在尝试使布局更简单: Sizer Alignment with all the input boxes aligned

注意文本是如何左对齐的,输入框是如何对齐的,我在 wxPython 演示代码中看到了这一点,但它们都使用了 flexgrid sizer,而我试图只使用 BoxSizers(因为它们更简单并且因为我只了解一点 sizer,甚至在使用 BoxSizers 时遇到困难,在 6 个月内我会遇到更困难的时间)

我曾尝试将输入和文本放在两个垂直尺寸器中,然后将它们放在水平尺寸器中,但没有用,因为文本未与输入对齐。我也尝试过这样做,并且还让每个文本、输入配对都放在一个 sizer 中,甚至更糟。有什么建议吗?

最佳答案

这是一个仅使用 BoxSizers 的简单示例:

import wx

class MyForm(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

# Add a panel so it looks the correct on all platforms
panel = wx.Panel(self, wx.ID_ANY)

# create the labels
lblOne = wx.StaticText(panel, label="labelOne", size=(60,-1))
lblTwo = wx.StaticText(panel, label="lblTwo", size=(60,-1))
lblThree = wx.StaticText(panel, label="lblThree", size=(60,-1))

# create the text controls
txtOne = wx.TextCtrl(panel)
txtTwo = wx.TextCtrl(panel)
txtThree = wx.TextCtrl(panel)

# create some sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
lineOneSizer = wx.BoxSizer(wx.HORIZONTAL)
lineTwoSizer = wx.BoxSizer(wx.HORIZONTAL)
lineThreeSizer = wx.BoxSizer(wx.HORIZONTAL)

# add widgets to sizers
lineOneSizer.Add(lblOne, 0, wx.ALL|wx.ALIGN_LEFT, 5)
lineOneSizer.Add(txtOne, 0, wx.ALL, 5)
lineTwoSizer.Add(lblTwo, 0, wx.ALL|wx.ALIGN_LEFT, 5)
lineTwoSizer.Add(txtTwo, 0, wx.ALL, 5)
lineThreeSizer.Add(lblThree, 0, wx.ALL|wx.ALIGN_LEFT, 5)
lineThreeSizer.Add(txtThree, 0, wx.ALL, 5)

mainSizer.Add(lineOneSizer)
mainSizer.Add(lineTwoSizer)
mainSizer.Add(lineThreeSizer)

panel.SetSizer(mainSizer)

# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

但这有点乱,所以这里有一个重构版本:

import wx

class MyForm(wx.Frame):

def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Tutorial")

# create the main sizer
self.mainSizer = wx.BoxSizer(wx.VERTICAL)

# Add a panel so it looks the correct on all platforms
self.panel = wx.Panel(self, wx.ID_ANY)

lbls = ["labelOne", "lblTwo", "lblThree"]
for lbl in lbls:
self.buildLayout(lbl)
self.panel.SetSizer(self.mainSizer)

#----------------------------------------------------------------------
def buildLayout(self, text):
""""""
lblSize = (60,-1)
lbl = wx.StaticText(self.panel, label=text, size=lblSize)
txt = wx.TextCtrl(self.panel)

sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(lbl, 0, wx.ALL|wx.ALIGN_LEFT, 5)
sizer.Add(txt, 0, wx.ALL, 5)
self.mainSizer.Add(sizer)

# Run the program
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()

关于python - wxPython GUI BoxSizers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3775071/

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