我想在StaticBitmap上創建懸停效果 - 如果鼠標光標位於位圖上,則顯示一個圖像,如果不是,則顯示第二個圖像。這是一個微不足道的程序(與一個按鈕完美配合)。但是,StaticBitmap不會發出EVT_WINDOW_ENTER,EVT_WINDOW_LEAVE事件。如何在wxpython的StaticBitmap上創建懸停效果?
我可以使用EVT_MOTION。如果圖像在光標位於圖像邊緣時切換,開關有時不起作用。 (主要是快速移動邊緣)。
示例代碼:
#!/usr/bin/python
# -*- coding: utf-8 -*-
import wx
def onWindow(event):
print "window event:", event.m_x, event.m_y
def onMotion(event):
print "motion event:", event.m_x, event.m_y
app = wx.App()
imageA = wx.Image("b.gif", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
imageB = wx.Image("a.gif", wx.BITMAP_TYPE_ANY).ConvertToBitmap()
frame = wx.Frame(None, wx.ID_ANY, title="Hover effect", size=(100+imageA.GetWidth(), 100+imageA.GetHeight()))
w = wx.Window(frame)
bmp = wx.StaticBitmap(w, -1, imageA, (50, 50), (imageA.GetWidth(), imageA.GetHeight()))
bmp.Bind(wx.EVT_MOTION, onMotion)
bmp.Bind(wx.EVT_ENTER_WINDOW, onWindow)
bmp.Bind(wx.EVT_LEAVE_WINDOW, onWindow)
frame.Show()
app.MainLoop()