如何在鼠标悬停时更改wx.Panel背景颜色?

6 投票
1 回答
15988 浏览
提问于 2025-04-15 19:21

这段代码:

import wx

app = None

class Plugin(wx.Panel):
    def __init__(self, parent, *args, **kwargs):
        wx.Panel.__init__(self, parent, *args, **kwargs)
        self.SetBackgroundColour((11, 11, 11))
        self.name = "plugin"

        self.Bind(wx.EVT_ENTER_WINDOW, self.onMouseOver)
        self.Bind(wx.EVT_LEAVE_WINDOW, self.onMouseLeave)

        wx.EVT_ENTER_WINDOW(self, self.onMouseOver)
        wx.EVT_LEAVE_WINDOW(self, self.onMouseLeave)

    def onMouseOver(self, event):
        self.SetBackgroundColor((179, 179, 179))
        self.Refresh()

    def onMouseLeave(self, event):
        self.SetBackgroundColor((11, 11, 11))
        self.Refresh()

    def OnClose(self, event):
        self.Close()
        app.Destroy()

    def name():
        print self.name


app = wx.App()
frame = wx.Frame(None, -1, size=(480, 380))
Plugin(frame)
frame.Show(True)
app.MainLoop()

给我带来了这个错误:

Traceback (most recent call last):
  File "C:\.... ... ....\plugin.py", line 18, in onMouseOver
    self.SetBackgroundColor((179, 179, 179))
AttributeError: 'Plugin' object has no attribute 'SetBackgroundColor'

我哪里做错了?

附注:我需要这个类作为一个 wx.Panel!

提前谢谢你!

1 个回答

15

这个方法叫做 SetBackgroundColour,注意是带一个“u”的。

另外,你在绑定事件的时候用了两种不同的方法,这样做是多余的。只需要用 self.Bind 这种方式就可以了,把其他两行代码删掉就行。

撰写回答