Pregunta

Deseo crear un efecto de desplazamiento en StaticBitmap: si el cursor del mouse está sobre el mapa de bits, muestra una imagen, si no, muestra la segunda imagen. Es un programa trivial (funciona perfectamente con un botón). Sin embargo, StaticBitmap no emite eventos EVT_WINDOW_ENTER, EVT_WINDOW_LEAVE.

Puedo trabajar con EVT_MOTION. Si las imágenes se cambian cuando el cursor está en el borde de la imagen, el cambio a veces no funciona. (Principalmente con movimientos rápidos sobre el borde).

Código de ejemplo:

#!/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()
¿Fue útil?

Solución

Parece que esto es un error wxGTK, los eventos ENTER y LEAVE funcionan bien en Windows. Debe dirigir la atención de los desarrolladores principales al problema, un buen lugar para hacerlo es su rastreador de errores . Este es un problema que no debería tener que evitar en mi humilde opinión.

He descubierto que los GenericButtons no tienen este problema en wxGTK, por lo que tal vez pueda usarlo hasta que se repare 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()

Otros consejos

Puede haber un error en la implementación de wxStaticBitmap, pero si wxBitmapButton funciona, puede usarlo para el mismo efecto, con menos código

#!/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()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top