I am using a searchctrl to find items in an EditableListbox. I also want to change the BackgroundColour of the ListBox if the item is found. That works well in the first place, but if I cancel the search and then repeat the search the BackgroundColour isn’t completly visible anymore. Does someome know how this could be done?
It seems the ItemColour overlays the BackgroundColour, is there probably a default value for the ItemColour which I could use for “m_searchCtrl_ELBOnCancelButton” ?
# -*- coding: utf-8 -*-
import wx
import wx.adv
## Class MainFrame ##
class MainFrame ( wx.Frame ):
def __init__( self, parent ):
wx.Frame.__init__ ( self, parent, id = wx.ID_ANY,
title = wx.EmptyString, pos = wx.DefaultPosition,
size = wx.Size( 500,300 ),
style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
self.SetSizeHints( wx.Size( 500,300 ), wx.DefaultSize )
self.SetBackgroundColour( wx.SystemSettings.GetColour( wx.SYS_COLOUR_HIGHLIGHT ) )
gSizer = wx.GridSizer( 1, 2, 3, 3 )
self.m_EDL1 = wx.adv.EditableListBox(
self, wx.ID_ANY, u"List_1", wx.DefaultPosition,
wx.DefaultSize,
style=wx.adv.EL_DEFAULT_STYLE |
#wx.adv.EL_NO_REORDER |
wx.adv.EL_ALLOW_NEW |
wx.adv.EL_ALLOW_EDIT |
wx.adv.EL_ALLOW_DELETE)
self.m_EDL1.SetStrings(["1a","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19",])
self.m_EDL1.SetBackgroundColour(wx.Colour(128,128,128))
gSizer.Add( self.m_EDL1, 1, wx.EXPAND|wx.ALL, 5 )
self.m_EDL2 = wx.adv.EditableListBox(
self, wx.ID_ANY, u"List_2", wx.DefaultPosition,
wx.DefaultSize,
style=wx.adv.EL_DEFAULT_STYLE |
#wx.adv.EL_NO_REORDER |
wx.adv.EL_ALLOW_NEW |
wx.adv.EL_ALLOW_EDIT |
wx.adv.EL_ALLOW_DELETE)
self.m_EDL2.SetStrings(["A","B","C","D","E","F","G","H","I","J",
"K","L","M","N","O","P","Q","R","S",])
self.m_EDL2.SetBackgroundColour(wx.Colour(128,128,128))
gSizer.Add( self.m_EDL2, 1, wx.EXPAND|wx.ALL, 5 )
self.SetSizer( gSizer )
self.Layout()
self.m_toolBar = self.CreateToolBar( wx.TB_HORIZONTAL, wx.ID_ANY )
self.m_toolBar.AddSeparator()
self.m_searchCtrl_ELB = wx.SearchCtrl( self.m_toolBar, wx.ID_ANY,
wx.EmptyString,
wx.DefaultPosition, wx.DefaultSize, 0 )
self.m_searchCtrl_ELB.ShowSearchButton( True )
self.m_searchCtrl_ELB.ShowCancelButton( True )
self.m_toolBar.AddControl( self.m_searchCtrl_ELB )
self.m_toolBar.AddSeparator()
self.m_toolBar.Realize()
self.Centre( wx.BOTH )
## Connect Events ##
self.m_searchCtrl_ELB.Bind( wx.EVT_SEARCHCTRL_CANCEL_BTN, self.m_searchCtrl_ELBOnCancelButton )
#self.m_searchCtrl_ELB.Bind( wx.EVT_TEXT_ENTER, self.m_searchCtrl_ELBOnTextEnter )
self.m_searchCtrl_ELB.Bind( wx.EVT_SEARCHCTRL_SEARCH_BTN, self.m_searchCtrl_ELBOnTextEnter )
def __del__( self ):
pass
## Virtual event handlers ##
def m_searchCtrl_ELBOnCancelButton( self, event ):
self.m_EDL1.ListCtrl.SetBackgroundColour(wx.Colour(255,255,255))
for i in range(len(self.m_EDL1.GetStrings())):
self.m_EDL1.ListCtrl.SetItemBackgroundColour(i,wx.Colour(255,255,255))
self.m_EDL1.Refresh()
event.Skip()
def m_searchCtrl_ELBOnTextEnter( self, event ):
search_term = self.m_searchCtrl_ELB.GetValue()
list_EDL1 = self.m_EDL1.GetStrings()
for i in range(len(list_EDL1)):
if search_term in list_EDL1[i]:
self.m_EDL1.ListCtrl.SetItemBackgroundColour(i,wx.Colour(125,255,125))
self.m_EDL1.ListCtrl.SetBackgroundColour(wx.Colour(220,255,220))
self.m_EDL1.Refresh()
event.Skip()
app = wx.App()
frame = MainFrame(None)
frame.Show(True)
app.MainLoop()