Reputation: 339
This is my code:
label_1 = wx.StaticText(self,-1,_("xxxxxxxxxxxxxxxxx"))
label_2 = wx.StaticText(self,-1,_("xxxxxxx: "))
self.item1= wx.TextCtrl(self,-1,"",style=wx.TE_PROCESS_ENTER)
self.item2= wx.TextCtrl(self, -1, "", style=wx.TE_PROCESS_ENTER)
self.item1.SetMinSize((200,-1))
self.item2.SetMinSize((200,-1))
sizer_1 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_1.Add(label_1,0,wx.RIGHT|wx.ALIGN_CENTER_VERTICAL,10)
sizer_1.Add(self.item1,0,wx.ALIGN_CENTER_VERTICAL)
sizer_2.Add(label_2,0,wx.RIGHT|wx.ALIGN_CENTER_VERTICAL,10)
sizer_2.Add(self.item2,0,wx.ALIGN_CENTER_VERTICAL)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(sizer_1,0,wx.ALL,4)
sizer.Add(sizer_2,0,wx.ALL,4)
self.SetSizer(sizer)
self.Layout()
Which gives:
Why the textctrl
aren't in the same distance from the labels? The labels have same length, the textctrl should "start" from their end.
How do I fix it?
Upvotes: 0
Views: 441
Reputation: 6206
In addition to the other answer and commentary about making it work with BoxSizer
, in my mind this example is crying out for a grid sizer. It will automatically align columns due to the nature of the grid, rather than needing the programmer to pound box sizers into submission. Here is your example ported to use a wx.FlexGridSizer
:
import wx
class Panel(wx.Panel):
def __init__(self, *args, **kw):
super(Panel, self).__init__(*args, **kw)
# Create widgets
label_1 = wx.StaticText(self, -1, "xxxxxxxxxxxxxxxxx:")
label_2 = wx.StaticText(self, -1, "xxxxxxx:")
self.item1 = wx.TextCtrl(self, -1, "", size=(200,-1), style=wx.TE_PROCESS_ENTER)
self.item2 = wx.TextCtrl(self, -1, "", size=(200,-1), style=wx.TE_PROCESS_ENTER)
# put them into a grid sizer
fgs = wx.FlexGridSizer(2,2, 10,10)
fgs.Add(label_1, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
fgs.Add(self.item1)
fgs.Add(label_2, 0, wx.ALIGN_RIGHT|wx.ALIGN_CENTER_VERTICAL)
fgs.Add(self.item2)
# put a margin around the whole thing
sizer = wx.BoxSizer()
sizer.Add(fgs, 1, wx.EXPAND|wx.ALL, 10)
self.SetSizer(sizer)
self.Layout()
app = wx.App()
frm = wx.Frame(None, title="foobar")
pnl = Panel(frm)
frm.Show()
app.MainLoop()
Upvotes: 1
Reputation: 33071
wxPython defaults will make widgets fit to their best size. The best way to get pairs of static text controls and text controls to line up is to set both of the StaticText widgets to a specific size via their size parameter. Something like this should suffice:
size = (150, -1)
label_1 = wx.StaticText(self,-1, label="xxxxxxxxxxxxxxxxx", size=size)
label_2 = wx.StaticText(self,-1, label="xxxxxxx:", size=size)
As you can see, this also removes the need to add padding to the second label's string.
Alternatively, you can use a GridSizer
(or one of its variants) and that will have the same effect.
Upvotes: 1