SearchCtrl text color

SearchCtrl shows text in two colors -- gray for the DescriptiveText
(e.g. "Search") and black for what the user types in. But if you use
the ChangeValue(new_text) method, it updates the text, but keeps the
current color. This means that if no text is showing in the search
field, new_text will show up gray. Even changing the text via the
keyboard doesn't change it back to black. Instead, it remains gray
until you delete all characters, change the focus from the SearchCtrl,
then put the focus back in the SearchCtrl. This is a problem for
users who naturally assume that the gray text means the control is
disabled.

I've tried forcing the text color in both the SearchCtrl and the
underlying TextCtrl, but it doesn't work. I'm running version
2.8.11.0 on Windows 7, if that makes any difference.

Here is code to see the problem.

import wx

class MyFrame(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, parent, title='Set SearchCtrl Value')
        self.panel = wx.Panel(self)
        self.button = wx.Button(self.panel, -1, 'Change search
string')
        self.search = wx.SearchCtrl(self.panel)
        self.static = wx.StaticText(self.panel, -1, 'Type string
here:')
        self.text = wx.TextCtrl(self.panel, -1, 'I was here')
        # Layout
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.search)
        sizer.Add(self.button)
        sizer.Add(self.static)
        sizer.Add(self.text)
        self.panel.SetSizer(sizer)
        sizer2 = wx.BoxSizer()
        sizer2.Add(self.panel, 1, wx.EXPAND, 0)
        self.SetSizer(sizer2)
        # Bind events
        self.Bind(wx.EVT_BUTTON, self.OnButton, self.button)

    def OnButton(self, event):
        self.search.ChangeValue(self.text.Value)

if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = MyFrame(None)
    app.SetTopWindow(frame)
    frame.Show()
    frame.Center()
    frame.Raise()
    app.MainLoop()

The generic version of the wx.SearchCtrl sets the lighter color from an EVT_IDLE handler if the widget is empty. It also sets the descriptive text at that time.

The foreground color is changed back to normal when the widget gets the focus and the current value is the descriptive text. It also sets the value to "" at that time.

So using that information you can work around the issue like this:

     def OnButton(self, event):
         self.search.ChangeValue(self.search.GetDescriptiveText())
         wx.CallAfter(self.doPart2)

     def doPart2(self):
         self.search.SetFocus()
         self.search.ChangeValue(self.text.Value)

The widget should probably also intercept SetValue and ChangeValue and take care of the colors there as needed. Please create a Trac ticket for this.

···

On 12/22/10 11:21 AM, Jim wrote:

SearchCtrl shows text in two colors -- gray for the DescriptiveText
(e.g. "Search") and black for what the user types in. But if you use
the ChangeValue(new_text) method, it updates the text, but keeps the
current color. This means that if no text is showing in the search
field, new_text will show up gray. Even changing the text via the
keyboard doesn't change it back to black. Instead, it remains gray
until you delete all characters, change the focus from the SearchCtrl,
then put the focus back in the SearchCtrl. This is a problem for
users who naturally assume that the gray text means the control is
disabled.

I've tried forcing the text color in both the SearchCtrl and the
underlying TextCtrl, but it doesn't work. I'm running version
2.8.11.0 on Windows 7, if that makes any difference.

--
Robin Dunn
Software Craftsman

Okay. I opened Ticket #12798 for this. Thanks for the workaround.
It works great!

···

On Dec 22, 5:32 pm, Robin Dunn <ro...@alldunn.com> wrote:

The widget should probably also intercept SetValue and ChangeValue and
take care of the colors there as needed. Please create a Trac ticket
for this.