4

作成したボタンのコレクションがあり、ボタンが押されたときにボタンの色を変更する必要があります。現在、デフォルトの色が設定されています (灰色 = 非アクティブ、水色 = アクティブ):

ここに画像の説明を入力

しかし、アクティブの色を赤に変更したいです。

これが私のボタンクラスです:

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.SetColour、を使用してみましたがself.button.SetBackgroundColourself.button.SetForegroundColourすべてうまくいきませんでした。wxpython 内でこれを達成する方法はありますか?

4

2 に答える 2

5

プラットフォームに依存しているようです。これは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
于 2011-11-01T16:11:55.547 に答える
0

SetBackgroundColour() は、Python 2.7.3 を使用した Windows 7 で RGB モード ((255,255,255) など) で色を使用して機能しました。

于 2014-03-21T11:09:48.270 に答える