如何在wxpython的StaticBitmap上创建悬停效果? [英] How to create hover effect on StaticBitmap in wxpython?

查看:821
本文介绍了如何在wxpython的StaticBitmap上创建悬停效果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在StaticBitmap上创建悬停效果 - 如果鼠标光标位于位图上,则显示一个图像,如果不是,则显示第二个图像。这是一个微不足道的程序(与一个按钮完美配合)。但是,StaticBitmap不会发出EVT_WINDOW_ENTER,EVT_WINDOW_LEAVE事件。



我可以使用EVT_MOTION。如果图像在光标位于图像边缘时切换,开关有时不起作用。 (主要是快速移动边缘)。



示例代码:

 #!/ usr / bin / python 
# - * - coding:utf-8 - * -

导入wx

def onWindow(event):
printwindow event:,event.m_x,event.m_y

def onMotion(event):
printmotion 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()
$ b $ frame = wx.Frame(None,wx.ID_ANY,title =悬停效果,size =(100 + imageA .GetWidth(),100 + imageA.GetHeight()))
$ bw = 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。绑定(wx.EVT_LEAVE_WINDOW,onWindow)

frame.Show()
app.MainLoop()


解决方案

它看起来像这是一个wxGTK错误,ENTER和LEAVE事件在Windows上工作正常。您应该引导核心开发人员注意这个问题,这样做的好地方是他们的错误跟踪器 。这是一个问题,你不应该工作在周围的恕我直言。

我发现GenericButtons在wxGTK没有这个问题,所以也许你可以使用它,直到StaticBitmap获取固定。

 #!/ usr / bin / python 
# - * - coding:utf-8 - * -

从wx.lib导入wx
导入按钮

def onWindow(event):
printwindow event:,event.m_x,event .m_y

def onMotion(event):
printmotion 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.B ind(wx.EVT_MOTION,onMotion)
bmp.Bind(wx.EVT_ENTER_WINDOW,onWindow)
bmp.Bind(wx.EVT_LEAVE_WINDOW,onWindow)

frame.Show()
app.MainLoop()


I want to create hover effect on StaticBitmap - If the cursor of mouse is over the the bitmap, shows one image, if not, shows second image. It's trivial program (works perfectly with a button). However, StaticBitmap doesn't emit EVT_WINDOW_ENTER, EVT_WINDOW_LEAVE events.

I can work with EVT_MOTION. If images are switched when the cursor is on the edge of image, switch sometimes doesn't work. (Mainly with fast moving over the edge).

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

解决方案

It looks like this is a wxGTK bug, ENTER and LEAVE events work fine on windows. You should direct the attention of the core developers to the problem, a good place to do this is their bug tracker. This is an issue you should not have to work around IMHO.

I have found that GenericButtons do not have this problem on wxGTK, so maybe you can use that until StaticBitmap gets fixed.

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

这篇关于如何在wxpython的StaticBitmap上创建悬停效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆