I have encountered a very strange problem in wxPython which I think is a bug. Essentially I have one TextCtrl which changes background color when I press the ctrl key (don't ask why). I then have another button that calls sizer.Layout() on the BoxSizer where the TextCtrl is stored. When I do this, the TextCtrl disappears (attains a zero width). It only happens if there is sufficient text in the TextCtrl, and only if I press ctrl+(other key). Here is the code:
import wx
class MyPanel(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.frame = parent
self.sizer = wx.BoxSizer(wx.VERTICAL)
self.btn = wx.Button(self, label="TextCtrl disappears?")
self.btn.Bind(wx.EVT_BUTTON, self.onClick)
self.txt=wx.TextCtrl(self,value="bunch of text goes here")
self.txt.Bind(wx.EVT_KEY_DOWN, self.onType)
self.sizer.Add(self.txt,0,wx.CENTER|wx.ALL,5)
self.sizer.Add(self.btn,0,wx.CENTER|wx.ALL,5)
self.SetSizer(self.sizer)
self.Fit()
def onType(self,event):
if event.GetKeyCode()==308:
self.txt.SetBackgroundColour((100,255,255,255))
else:
event.Skip()
def onClick(self,event):
self.sizer.Layout()
class MyFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, parent=None, title="")
panel = MyPanel(self)
self.Fit()
self.Show()
app = wx.App(False)
frame = MyFrame()
app.MainLoop()
Now, try to go to the TextCtrl and press for instance ctrl+a, and then press the button. Is this a bug? More importantly, what is the workaround? I guess that the TextCtrl changes one of its attributes that specifies its size or behaviour under Layout(), but I can not figure out which one is being changed.
edit:
I am using ubuntu 16.04, Python 2.7.12 and wxpython version 3.0.2.0.
Whatever it is, it has been resolved in wxpython 4.
Ways to avoid the problem.
Give the TextCtrl
a size=(n,n)
value
and / or
Replace the wx.CENTER
with a wx.EXPAND
when adding to the sizer.
i.e.
self.txt=wx.TextCtrl(self,-1,value="bunch of text goes here",size=(50,20))
self.sizer.Add(self.txt,0,wx.EXPAND|wx.ALL,5)
Finally, there must be something queer about your keyboard, because this should not happen using EVT_KEY_DOWN
but with EVT_KEY_UP
. I don't think that EVT_KEY_DOWN
normally sees a Ctrl key. See https://www.wxpython.org/Phoenix/docs/html/wx.KeyEvent.html