【问题标题】:How do I set the color of a toggle button in wxpython?如何在 wxpython 中设置切换按钮的颜色?
【发布时间】:2011-10-15 12:05:09
【问题描述】:

我有一个我创建的按钮集合,并且需要在按下按钮时更改按钮的颜色。目前它设置了默认颜色(灰色 = 不活动;浅蓝色 = 活动):

但我想将活动的颜色更改为红色。

这是我的按钮类:

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')

我尝试过使用 self.button.SetColourself.button.SetBackgroundColourself.button.SetForegroundColour 所有这些都没有成功。有没有办法在 wxpython 中实现这一点?

【问题讨论】:

  • 我相当有信心这是特定于操作系统/主题的,并且您无法控制它,但我不能 100% 确定这一点。
  • @g.d.d.c - 我很害怕,但不确定。
  • 让我运行一些测试,我的一个项目中有几个 ToggleButtons。几分钟后我会在这里更新我的发现。
  • 我同样无法控制这些控件的突出显示颜色。 SetOwnBackgroundColor 似乎设置了包含大小调整器中按钮后面区域的颜色,但我不能影响任何标准方法中的实际按钮。我倾向于不可能,但我要主演这个以防其他人有办法。
  • Robin Dunn 的这句话表明这是 wxWidgets groups.google.com/group/wxpython-users/msg/ac60f3a1ef05197b 的限制。

标签: button colors wxpython togglebutton


【解决方案1】:

它似乎依赖于平台。这对我在 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')

解决方法:

    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

【讨论】:

    【解决方案2】:

    SetBackgroundColour() 在带有 python 2.7.3 的 Windows 7 中使用 RGB 模式下的颜色(如 (255,255,255))为我工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 2013-06-07
      相关资源
      最近更新 更多