4

我想在 StaticBitmap 上创建悬停效果 - 如果鼠标光标在位图上,则显示一个图像,如果不是,则显示第二个图像。这是一个简单的程序(与按钮完美配合)。但是,StaticBitmap 不会发出 EVT_WINDOW_ENTER、EVT_WINDOW_LEAVE 事件。

我可以使用 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()
4

2 回答 2

1

看起来这是一个 wxGTK 错误,ENTER 和 LEAVE 事件在 Windows 上运行良好。您应该将核心开发人员的注意力引导到问题上,这样做的好地方是他们的错误跟踪器。这是一个你不应该解决恕我直言的问题。

我发现 GenericButtons 在 wxGTK 上没有这个问题,所以也许你可以使用它,直到 StaticBitmap 得到修复。

#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx
from wx.lib import buttons

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 = buttons.GenBitmapButton(w, -1, imageA, style=wx.BORDER_NONE)
#bmp.Bind(wx.EVT_MOTION, onMotion)
bmp.Bind(wx.EVT_ENTER_WINDOW, onWindow)
bmp.Bind(wx.EVT_LEAVE_WINDOW, onWindow)

frame.Show()
app.MainLoop()
于 2009-09-12T18:44:06.600 回答
0

wxStaticBitmap 实现中可能存在错误,但是如果 wxBitmapButton 有效,您可以使用它来获得相同的效果,并且代码更少

#!/usr/bin/python
# -*- coding: utf-8 -*-

import wx

app = wx.App()

frame = wx.Frame(None, wx.ID_ANY, title="Hover effect")
w = wx.Window(frame)
c = wx.BitmapButton(w, -1, wx.EmptyBitmap(25,25), style = wx.NO_BORDER)
c.SetBitmapHover(wx.EmptyBitmap(3,3))
frame.Show()

app.MainLoop()
于 2009-09-13T04:13:45.213 回答