質問

StaticBitmapにホバー効果を作成したい-マウスのカーソルがビットマップ上にある場合、1つの画像を表示し、そうでない場合は2番目の画像を表示します。それは簡単なプログラムです(ボタンで完璧に動作します)。ただし、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()
役に立ちましたか?

解決

これはwxGTKのバグのようです。WindowsではENTERおよびLEAVEイベントが正常に機能します。コア開発者の注意を問題に向ける必要があります。これを行うには、バグトラッカーが最適です。 。これは、IMHOを回避する必要がない問題です。

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()

他のヒント

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()
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top