如何在wxpython中的StaticBitmap上创建悬停效果?

发布于 2024-08-04 03:43:50 字数 955 浏览 3 评论 0原文

我想在 StaticBitmap 上创建悬停效果 - 如果鼠标光标位于位图上,则显示一张图像,如果没有,则显示第二张图像。这是一个简单的程序(与按钮完美配合)。但是,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()

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菩提树下叶撕阳。 2024-08-11 03:43:50

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

我发现 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()

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()
旧伤慢歌 2024-08-11 03:43:50

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

There may be bug in wxStaticBitmap implementation, but if wxBitmapButton works you can use it for same effect, with less 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文