Jam*_*rtz 4 wxpython colors button togglebutton
我有一个已创建的按钮集合,需要在按下按钮时更改按钮的颜色。当前,它设置默认颜色(灰色=无效;浅蓝色=有效):

但我想将活动颜色更改为红色。
这是我的按钮类:
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')
Run Code Online (Sandbox Code Playgroud)
我已经尝试使用self.button.SetColour,self.button.SetBackgroundColour,self.button.SetForegroundColour所有这一切都没有成功。有没有办法在wxpython中完成此操作?
似乎取决于平台。这在Ubuntu中对我有用,但在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')
Run Code Online (Sandbox Code Playgroud)
解决方法:
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
Run Code Online (Sandbox Code Playgroud)