Custom derived StatusBar refresh flickering

I have derived a status bar from wx.StatusBar shown below because I need to
make my status bar flash a different colour under certain conditions. I
periodically (every half a second) call the update() method from the main
program. The bar keeps flickering every 4 to 10 seconds for some reason
whether it is in a flashing state or not and I can't find a solution. I've
tried wrapping Freeze and Thaw round the cal to update() but to no avail.

class ClientStatusBar(wx.StatusBar):
    def __init__(self, parent):
        super(ClientStatusBar, self).__init__(parent)
        self.Bind(wx.EVT_PAINT, self.OnPaint)
        self.parent = parent
        self.dead_ports = []
        self.dead_nodes = []
        
    def OnPaint(self,event):
        dc = wx.PaintDC(self)
        self.Draw(dc)
        
    def Draw(self, dc):
        dc.BeginDrawing()
        dc.SetBackground(wx.Brush(self.get_colour()))
        dc.Clear()
        #dc.SetPen(wx.Pen('Black'))
        dc.SetTextForeground("Light Grey")
        dc.DrawText(self.get_status_text(), 10, 4)
        time = self.get_time()
        w, h = self.GetRect().width, self.GetRect().height
        tw, th = dc.GetTextExtent(time)
        tposx = w - tw - 10
        dc.DrawText(time, tposx, 4)
        dc.EndDrawing()
        
    def get_colour(self):
        if self.dead_nodes and self.parent.flash_state == constants.on:
            return "Red"
        else:
            return "Dark Grey"
            
    def get_status_text(self):
        if self.dead_nodes:
            t = "Connection Lost to Node"
            t_end = "s: "
            if len(self.dead_nodes) == 1:
                t_end = ": "
            return t + t_end + ", ".join(self.dead_nodes)
        else:
            return "Connection OK"
        
    def get_time(self):
        t = time.localtime(time.time())
        st = time.strftime("%d-%b-%Y %H:%M:%S", t)
        return st
        
    def update(self, dead_ports, dead_nodes):
        self.dead_ports = dead_ports
        self.dead_nodes = dead_nodes
        self.Refresh()

Many Thanks
Craig

CraigDouglas wrote:

I have derived a status bar from wx.StatusBar shown below because I need to make my status bar flash a different colour under certain conditions. I periodically (every half a second) call the update() method from the main program. The bar keeps flickering every 4 to 10 seconds for some reason whether it is in a flashing state or not and I can't find a solution. I've tried wrapping Freeze and Thaw round the cal to update() but to no avail.

Refresh() doesn't actually do any drawing, it just requests that a paint event be sent soon, so wrapping it in Freeze/Thaw won't help. Try passing False to Refresh, that will cause it to not erase the widget before painting it, and that is probably the cause of the flicker you are seeing.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!