I'm trying to create a full screen window in which statictextctrl with quite long label are layed out in a table, with vertical scrollbar when there are a lot of them. What I need to do is wrap the label accordingly to the width that is available to each statictextctrl (based on the number of textctrl per row and the hpad). Unfortunately, when there is a very long word, I can't make it work, the word is not wraped, even if I set explicitely the textctrl style to wx.TE_BESTWRAP
(which is default anyway, but I thought it was worth trying). Any idea how to achieve this?
import wx
import wx.lib.scrolledpanel as scrolled
class MyPanel(scrolled.ScrolledPanel):
def __init__(self, parent):
scrolled.ScrolledPanel.__init__(self, parent, -1)
## Configuring the panel
self.SetAutoLayout(1)
self.SetupScrolling()
self.SetScrollRate(1,40)
# needs to be called after main window's laytou, so its size is actually
# known and can be used to compute textctrl's width
def Build(self):
labelPerRow=7
hgap = 40
vgap = 20
label_width=int(self.GetClientSize()[0]/labelPerRow)-hgap
print("label width", label_width)
grid_sizer = wx.FlexGridSizer(labelPerRow, vgap, hgap)
self.SetSizer(grid_sizer)
i=0
for label in range(200) :
label = "very long title withaverybigwordthatdoesntfitonasinglelinesoitsquitehardtomanagewordwrap"+str(i)
title = wx.StaticText(self,
label=label,
style=wx.ALIGN_CENTRE_HORIZONTAL |
wx.TE_BESTWRAP)
title.Wrap(int(label_width))
grid_sizer.AddMany([(title)])
i = i+1
self.Layout()
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Test", style=wx.NO_BORDER)
self.Maximize(True)
frameSizer = wx.BoxSizer()
p = MyPanel(self)
frameSizer.Add(p, 1, wx.EXPAND)
self.SetSizer(frameSizer)
self.Layout()
p.Build()
app = wx.App(0)
frame = MyFrame()
frame.Show()
app.MainLoop()
Here is what I came up with. I hope it will be usefull to others. Basically, it computes every combinations of splitting either with
'\n'
or' '
, keeps the label for which the width fits and with minimum height. If no wrapping fits, it uses the label with the smallest width, and removes the proper characters at the end, replacing them with an ellipsis, for the label to fit. It could be improved, for example with a max number of rows or more options for text alignment, but it proved effective so far. Note that computing all the combinations may be time-consuming if the unwrapped label is long (the number of combinations being the exponential of the number of spaces).