Search code examples
pythonwxpython

How to change wxpython gizmo LED color when button is clicked


I want to change the colour of a wx gizmos led when the wx button is clicked.

My example is as follows.

import wx
import wx.lib.gizmos.ledctrl as led

class Main(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, parent = None, title ="LED test")
        panel = wx.Panel(self)
        self.myLED = led.LEDNumberCtrl(panel, -1, pos = (150,50), size = (100,100))
        self.myLED.SetBackgroundColour("gray")
        self.myButton = wx.Button(panel, -1,  "myButton", pos =(50, 50))
        self.myButton.Bind(wx.EVT_BUTTON, self.changeLEDColor)

    def changeLEDColor(self,event):
        self.myLED.SetBackgroundColour("green")


if __name__ == "__main__":
    app = wx.App()
    frame = Main()
    frame.Show()
    app.MainLoop()

I expected the led colour to change to 'green', when I click 'mybutton', but it is still 'gray'.

What's wrong with my example?


Solution

  • Adding self.Refresh() or self.myLED.Refresh() will trigger the repaint. Here's the link to the docs. If it flickers, look into wx.Frame.SetDoubleBuffered(True) - docs