python 如何更改 MouseOver 上的 wx.Panel 背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2275917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change wx.Panel background color on MouseOver?
提问by aF.
this code:
这段代码:
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()
gives me the error:
给我错误:
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'
What am I doing wrong? P.S.: I need to have this class as a wx.Panel!
我究竟做错了什么?PS:我需要将这个类作为 wx.Panel !
Thanks in advance
提前致谢
回答by FogleBird
The method is named SetBackgroundColour
, with a u.
该方法名为SetBackgroundColour
,带有 u。
Also, you're binding events twice with two different methods. Just use the self.Bind
style, and remove the other two lines.
此外,您使用两种不同的方法两次绑定事件。只需使用self.Bind
样式,并删除其他两行。