I have a TextCtrl with style=wx.TE_CENTRE, and I want/expect its contents to remain centered when I resize the TextCtrl. However, the text stays on the left side of the TextCtrl.
import wx
class MyApp(wx.App):
def OnInit(self):
frame = wx.Frame(None)
frame.Show(True)
self.SetTopWindow(frame)
self.myText = wx.TextCtrl(frame, value="A", style=wx.TE_CENTRE)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.myText, wx.EXPAND)
frame.SetSizer(sizer)
sizer.Fit(frame)
self.Bind(wx.EVT_SIZE, self.OnSize)
return True
def OnSize(self, evt):
self.myText.SetValue("after resize")
myApp = MyApp(0)
myApp.MainLoop()
After a lot of trial and error, I got it to work by doing both of the following:
- Refresh the TextCtrl in my wx.EVT_SIZE handler
- Add the style wx.TE_RICH to the TextCtrl
Why are both of these necessary? I'd like to understand what I'm doing wrong here. (I'm using wxPython 4.0.4 on Win7.)