2009-09-12 9 views
4

Je veux créer un effet de survol sur StaticBitmap - Si le curseur de la souris est sur le bitmap, montre une image, sinon, montre la deuxième image. C'est un programme trivial (fonctionne parfaitement avec un bouton). Toutefois, StaticBitmap n'émet pas d'événements EVT_WINDOW_ENTER, EVT_WINDOW_LEAVE.Comment créer un effet de survol sur StaticBitmap dans wxpython?

Je peux travailler avec EVT_MOTION. Si les images sont commutées lorsque le curseur est sur le bord de l'image, le commutateur ne fonctionne parfois pas. (Principalement avec le déplacement rapide sur le bord).

code Exemple:

#!/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() 

Répondre

1

Il ressemble à ceci est un bug wxGTK, événements entrée et de sortie fonctionnent très bien sur les fenêtres. Vous devez diriger l'attention des développeurs de base sur le problème, un bon endroit pour le faire est leur bug tracker. C'est un problème que vous ne devriez pas avoir à travailler autour de mon humble avis.

J'ai trouvé que GenericButtons n'a pas ce problème sur wxGTK, alors peut-être que vous pouvez l'utiliser jusqu'à ce que StaticBitmap soit corrigé.

#!/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() 
0

Il peut y avoir bogue dans la mise en œuvre wxStaticBitmap, mais si wxBitmapButton fonctionne, vous pouvez l'utiliser pour 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() 
Questions connexes