Comment créer un effet de survol sur StaticBitmap dans wxpython?
Question
Je veux créer un effet de survol sur StaticBitmap - Si le curseur de la souris est sur la bitmap, affiche une image, sinon une seconde image. C'est un programme trivial (fonctionne parfaitement avec un bouton). Cependant, StaticBitmap n'émet pas les événements EVT_WINDOW_ENTER, EVT_WINDOW_LEAVE.
Je peux travailler avec EVT_MOTION. Si les images sont commutées alors que le curseur est sur le bord de l’image, les commutations ne fonctionnent parfois pas. (Principalement avec un mouvement rapide sur le bord).
Exemple de code:
#!/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()
La solution
On dirait qu’il s’agit d’un bogue wxGTK, les événements ENTER et LEAVE fonctionnent correctement sous Windows. Vous devez attirer l'attention des principaux développeurs sur le problème. Un gestionnaire de bogues est un bon endroit pour le faire. . C’est un problème pour lequel vous ne devriez pas avoir à travailler à mon humble avis.
J'ai constaté que GenericButtons ne rencontrait pas ce problème sur wxGTK. Vous pouvez donc l'utiliser jusqu'à ce que StaticBitmap soit résolu.
#!/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()
Autres conseils
Il peut y avoir un bogue dans l'implémentation de wxStaticBitmap, mais si wxBitmapButton fonctionne, vous pouvez l'utiliser pour le même effet, avec moins de code
#!/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()