Reputation: 332
#! /usr/bin/python
#SearchCtrlProblem
import wx
class ControlPanel(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(200,200))
self.panel = wx.Panel(self, -1)
vbox = wx.BoxSizer(wx.VERTICAL)
self.panel.SetSizer(vbox)
self.DoLayout()
self.Centre()
self.Show(True)
def DoLayout(self):
vbox = self.panel.GetSizer()
self.tc = tc = wx.TextCtrl(self.panel, size = (140,-1), style=wx.TE_PROCESS_ENTER)
vbox.Add(tc, 0, wx.ALL, 15)
tc.Bind(wx.EVT_TEXT_ENTER, self.OnTextCtrl, id=tc.GetId())
self.sc = sc = wx.SearchCtrl(self.panel, size = (140,-1), style=wx.TE_PROCESS_ENTER)
sc.ShowSearchButton(True)
sc.ShowCancelButton(True)
vbox.Add(sc, 0, wx.ALL, 15)
sc.Bind(wx.EVT_TEXT_ENTER, self.OnSearchCtrl, id=sc.GetId())
self.bn = bn = wx.Button(self.panel, -1, 'Reset', (140,-1))
vbox. Add(bn, 0, wx.ALL,15)
bn.Bind(wx.EVT_BUTTON, self.OnReset, id = bn.GetId())
vbox.Layout()
self.Refresh()
def OnTextCtrl(self, evt):
value = self.tc.GetValue()
self.sc.SetValue(value)
def OnSearchCtrl(self, evt):
value = self.sc.GetValue()
self.tc.SetValue(value)
def OnReset(self, evt):
self.panel.DestroyChildren()
self.DoLayout()
app = wx.App()
ControlPanel(None, -1, '')
app.MainLoop()
I wrote this silly program to demonstrate the question. + When I run program, if I start by typing into the search box sc then press enter, everything is OK with black text in sc. Now, I can type in text box tc then press enter, text in sc still black
Note: before start typing, you can reset the layout by pressing on the button "Reset". The problem happened on Linux RedHat 4.5.1-3, Python 2.7. When I tried this on Mac OS X 10.8, Python 2.7.2, this problem didn't happen.
How can I make the text in SearchCtrl always black?
Upvotes: 1
Views: 523
Reputation: 6226
This is a bug in wxWidgets 2.8.12
.
The control will only set the text color to black when it receives focus while the text matches the hint text (the grey text shown when the control is empty).
To fix this without upgrading to a never version, you have to focus the control before changing its text:
wnd = self.FindFocus() # get currently focused window
self.sc.SetFocus() # trigger the color update (if needed)
self.sc.SetValue(value)
if wnd: wnd.SetFocus() # restore previous focus
else: self.SetFocus() # set focus to frame if none was set
To remove the text from the wxSearchCtrl and restore the grayed hint text, a simple call to Clear()
will do (in 2.8.12 anyway).
That bug is fixed in the latest development release (2.9.4
). However, clearing the text does not restore the hint text.
On OS X 10.3
and later, the native search control is used which does not exhibit this behavior.
Upvotes: 2