How to create hover effect on StaticBitmap in wxpython?(如何在 wxpython 中的 StaticBitmap 上创建悬停效果?)
问题描述
我想在 StaticBitmap 上创建悬停效果 - 如果鼠标光标在位图上,则显示一个图像,如果不是,则显示第二个图像.这是一个简单的程序(与按钮完美配合).但是,StaticBitmap 不会发出 EVT_WINDOW_ENTER、EVT_WINDOW_LEAVE 事件.
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.
我可以使用 EVT_MOTION.如果光标在图像边缘时切换图像,有时切换不起作用.(主要是在边缘快速移动).
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).
示例代码:
#!/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 错误,ENTER 和 LEAVE 事件在 Windows 上运行良好.您应该将核心开发人员的注意力引向问题,这样做的好地方是他们的 bug tracker.恕我直言,这是您不应该解决的问题.
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.
我发现 GenericButtons 在 wxGTK 上没有这个问题,所以也许你可以使用它直到 StaticBitmap 得到修复.
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 上创建悬停效果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 wxpython 中的 StaticBitmap 上创建悬停效果?


基础教程推荐
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01