I’m trying make a static text which if too long for the sizer, will truncate itself with an ellipse and just show the text which will fit.
What I’ve written seems to work fine when you change the size of the window which must be sending paint events, but when setting the label text I guess a paint event doesn’t get fired.
I’ve tried calling Refresh and Update on everything I can thing of (Frame, panel, the text itself) as well as sending a paint event to the frame but I cant get it to work correctly.
I made a quick demo with a button to change the text label.
EDIT: SOLVED
import wx
from wx.lib.stattext import GenStaticText as StaticText
class EllipticStaticText2(StaticText):
def __init__(self,parent,id=wx.ID_ANY,label='',width=None,pos=wx.DefaultPosition,size=wx.DefaultSize,style=0,name="ellipticstatictext2"):
self.actual_label= label
self.parent= parent
self.width= width
StaticText.__init__(self,parent,id,label,pos,size,style,name)
self.Bind(wx.EVT_PAINT, self.OnPaint2)
def SetLabel(self,label):
self.actual_label= label
StaticText.SetLabel(self,label)
self.parent.Layout()
def SetLabelText(self,label):
self.actual_label= label
StaticText.SetLabelText(self,label)
self.parent.Layout()
def OnPaint2(self,event):
dc= wx.PaintDC(self)
displaylabel= self.actual_label
x,y= dc.GetTextExtent(displaylabel)
self.w,self.h= self.GetSize()
if x>self.w:
while x>self.w:
displaylabel= displaylabel[:-1]
x,y= dc.GetTextExtent(displaylabel+"...")
StaticText.SetLabel(self,displaylabel+"...")
else:
StaticText.SetLabel(self,displaylabel)
event.Skip()
class CustomFrame(wx.Frame):
def __init__(self,parent,id,name,size):
wx.Frame.__init__(self,parent,id,name,size)
self.panel = wx.Panel(self, -1)
sizer = wx.BoxSizer(wx.VERTICAL)
self.index= 35
self.elliptic = EllipticStaticText2(self.panel, -1, r"long string."*20)
whitePanel = wx.Panel(self.panel, -1)
whitePanel.SetBackgroundColour(wx.WHITE)
sizer.Add(self.elliptic, 0, wx.ALL|wx.EXPAND, 10)
btn= wx.Button(self.panel,-1,"change")
sizer.Add(btn,0,wx.ALL|wx.EXPAND, 10)
sizer.Add(whitePanel, 1, wx.ALL|wx.EXPAND, 10)
btn.Bind(wx.EVT_BUTTON,self.button)
self.panel.SetSizer(sizer)
sizer.Layout()
def button(self,event):
self.elliptic.SetLabel(chr(self.index)*100)
self.index+=1
self.Refresh()
self.panel.Refresh()
self.elliptic.Refresh()
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = CustomFrame(None, -1, "EllipticStaticText", size=(700, 300))
frame.CenterOnScreen()
frame.Show()
app.MainLoop()
Edited the code above which now works nicely, calling
Layout()on the text’s parent did the trick.. I keep forgetting about that method!