with this code:
import wx
class Plugin(wx.Panel):
def __init__(self, parent, *args, **kwargs):
panel = wx.Panel.__init__(self, parent, *args, **kwargs)
self.colorOver = ((89,89,89))
self.colorLeave = ((110,110,110))
self.colorFont = ((145,145,145))
self.SetBackgroundColour(self.colorLeave)
self.SetForegroundColour(self.colorLeave)
self.name = "Plugin"
self.overPanel = 0
self.overLabel = 0
self.overButton = 0
sizer = wx.BoxSizer(wx.VERTICAL)
self.pluginName = wx.StaticText(self, -1, ' ' + self.getName())
self.pluginClose = wx.BitmapButton(self, -1, wx.Bitmap('C:\Users\André Ferreira\Desktop\Tese\Código Python\SoundLog\Images\close.png'), style=wx.NO_BORDER)
self.pluginClose.Hide()
gs = wx.GridSizer(2, 2, 0, 0)
gs.AddMany([(self.pluginName, 0, wx.ALIGN_LEFT), (self.pluginClose, 0, wx.ALIGN_RIGHT|wx.CENTER)])
sizer.Add(gs, 1, wx.EXPAND)
self.SetSizer(sizer)
self.pluginName.Bind(wx.EVT_LEAVE_WINDOW, self.onLabelMouseLeave)
self.pluginName.Bind(wx.EVT_ENTER_WINDOW, self.onLabelMouseOver)
self.pluginClose.Bind(wx.EVT_BUTTON, self.onCloseMouseClic开发者_开发知识库k)
self.Bind(wx.EVT_LEAVE_WINDOW, self.onPanelMouseLeave)
self.Bind(wx.EVT_ENTER_WINDOW, self.onPanelMouseOver)
def onPanelMouseOver(self, event):
self.overPanel = 1
self.overLabel = 0
self.SetBackgroundColour(self.colorOver)
self.pluginName.SetForegroundColour(self.colorFont)
self.Refresh()
self.pluginClose.Show()
def onPanelMouseLeave(self, event):
if self.overLabel == 0:
self.overPanel = 0
self.SetBackgroundColour(self.colorLeave)
self.pluginName.SetForegroundColour(self.colorLeave)
self.Refresh()
self.pluginClose.Hide()
def onLabelMouseOver(self, event):
self.overPanel = 0
self.overLabel = 1
self.SetBackgroundColour(self.colorOver)
self.pluginName.SetForegroundColour(self.colorFont)
self.Refresh()
self.pluginClose.Show()
def onLabelMouseLeave(self, event):
if self.overPanel == 0:
self.overLabel = 0
self.SetBackgroundColour(self.colorLeave)
self.pluginName.SetForegroundColour(self.colorLeave)
self.Refresh()
self.pluginClose.Hide()
def OnClose(self, event):
self.Close()
app.Destroy()
def onCloseMouseClick(self, event):
self.Hide()
def getName(self):
return self.name
whenever I get over the BitmapButton the next two events:
self.Bind(wx.EVT_LEAVE_WINDOW, self.onPanelMouseLeave)
self.Bind(wx.EVT_ENTER_WINDOW, self.onPanelMouseOver)
are allways beeing called.
What is wrong with it? How can only one event be called?
Note: if I bind the leave and enter window events to the BitmapButton the two previous events are still called.
Problem is you are hiding and showing bitmap on mouseover, so when you move mouseover bitmap, mouse goes off panel, onPanelMouseLeave is called and you hide bitmap, which means now mouse if over panel, onPanelMouseOver is called and in that event you again show bitmap and that means now mouse if over bitmap again and out of panel and hence you are stuck in a hide/show circle.
Comment out self.pluginClose.Hide() in onPanelMouseLeave and you won't see multiple events.
Try to come with a different approach e.g. check if mouse if over bitmap and hence do not hide it.
精彩评论