Frage

Ich mag Hover-Effekt auf StaticBitmap erstellen - Wenn die Cursor der Maus über das Bitmap sind, ein Bild zeigt, wenn nicht, zweites Bild zeigt. Es ist trivial Programm (funktioniert perfekt mit einer Taste). Allerdings StaticBitmap nicht emittieren EVT_WINDOW_ENTER, EVT_WINDOW_LEAVE Ereignisse.

Ich kann mit EVT_MOTION arbeiten. Wenn Bilder umgeschaltet werden, wenn sich der Cursor am Rand des Bildes ist, manchmal Schalter nicht funktioniert. (Vor allem bei schnell bewegten über den Rand).

Beispielcode:

#!/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()
War es hilfreich?

Lösung

Es sieht aus wie dies ein wxGTK Fehler ist, betreten und verlassen Ereignisse funktionieren unter Windows. Sie sollten die Aufmerksamkeit der Kern-Entwickler auf das Problem lenken, ein guter Ort, um dies zu tun, ist ihre Bug-Tracker rel="nofollow . Dies ist ein Problem, das Sie sollten nicht umgehen IMHO haben.

Ich habe festgestellt, dass GenericButtons haben dieses Problem nicht auf wxGTK, vielleicht können Sie verwenden, dass bis StaticBitmap behoben wird.

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

Andere Tipps

Es kann Fehler in wxStaticBitmap Implementierung sein, aber wenn wxBitmapButton funktioniert können Sie es für denselben Effekt, mit weniger Code verwenden

#!/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()
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top