Dev-Picayune

picayune: of little value or importance

Another wxPython Little Trick

In what is probably a somewhat unusual manuever, I needed to change the background color of a static text object (an entry field’s label) to a highlight color when the entry field has focus and then back to normal again when the entry field loses focus. I subclassed the TextCtrl and then gave it an instance attribute for storing the label control so that it could affect the label for me. Within my handler for the wx.EVT_SET_FOCUS I have the following code:

     def _onFocus(self,evt):                 self._labelCtrl.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENUHILIGHT))         self._labelCtrl.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHTTEXT))         self._labelCtrl.Refresh() 


And then to handle the loss of focus (wx.EVT_KILL_FOCUS) I handle it like this:

     def _onLoseFocus(self,evt):                         self._labelCtrl.SetBackgroundColour(None)                self._labelCtrl.SetForegroundColour(None)         self._labelCtrl.Refresh() 


Originally, I tried to store the original colors using the GetBackgroundColour() method but apparently between my control being on a notebook tab page and then the drawing of the control, the color reported back was not completely accurate because upon losing focus, it turned a slightly different color (darker like a regular panel instead of tab panel). Of course, this is on my XP machine, I have not looked to see what happens under Linux or Win2K. That’s when I thought to try good ‘ole None and poof, problems solved because that apparently resets it to whatever the system wants to use. Also of note, is the Refresh() method. Without the Refresh() it appears to not redraw the control with the new colors.

No comments yet. Be the first.

Leave a reply

For spam filtering purposes, please copy the number 3845 to the field below: