How to set the color of a toggle button in wxpython?

I have a set of buttons that I created, and you need to change the color of the button when pressed. It currently sets the default colors (gray = inactive, light blue = active):

enter image description here

but I want to change the color of the active to red.

Here is my button class:

class ButtonClass(wx.Panel): def __init__(self, parent, name, id): wx.Panel.__init__(self, parent) self.name = name self.taskid = id self.button = wx.ToggleButton(self, 1, size=(50, 50)) self.button.SetLabel('Start') self.mainSizer = wx.BoxSizer(wx.HORIZONTAL) self.mainSizer.Add(self.button) self.Bind(wx.EVT_TOGGLEBUTTON, self.toggledbutton, self.button) # Where the buttons change state def toggledbutton(self, event): # Active State if self.button.GetValue() == True: self.button.SetLabel('Stop') # Inactive State if self.button.GetValue() == False: self.button.SetLabel('Start') 

I tried using self.button.SetColour , self.button.SetBackgroundColour , self.button.SetForegroundColour , all of which were not successful. Is there a way to do this in wxpython?

+4
source share
2 answers

It seems to be platform dependent. This worked for me on Ubuntu, but not on Windows.

 self.ToggleButtonObj = wx.ToggleButton(self, -1, 'ButtonLabel') self.ToggleButtonObj.Bind(wx.EVT_TOGGLEBUTTON, self.OnToggleClick) def OnToggleClick(self,event): if self.ToggleButtonObj.GetValue(): self.ToggleButtonObj.SetBackgroundColour('#color1') else: self.ToggleButtonObj.SetBackgroundColour('#color2') 

Workaround:

  self.Button = wx.Button(self, -1, 'ButtonLabel') self.Button.Bind(wx.EVT_BUTTON, self.OnToggleClick) self.ButtonValue = False def OnToggleClick(self,event): if not self.ButtonValue(): self.Button.SetBackgroundColour('#color1') self.ButtonValue = True else: self.Button.SetBackgroundColour('#color2') self.ButtonValue = False 
+3
source

SetBackgroundColour () worked for me using color in RGB mode (e.g. (255,255,255)) on Windows 7 using python 2.7.3.

0
source

All Articles