如何修复wxpython事件调用?

发布于 2024-08-22 22:42:24 字数 2932 浏览 8 评论 0原文

使用以下代码:

import wx


class Plugin(wx.Panel):

    def __init__(self, parent, *args, **kwargs):
        panel = wx.Panel.__init__(self, parent, *args, **kwargs)
        self.colorOver = ((89,89,89))
        self.colorLeave = ((110,110,110))
        self.colorFont = ((145,145,145))
        self.SetBackgroundColour(self.colorLeave)
        self.SetForegroundColour(self.colorLeave)
        self.name = "Plugin"
        self.overPanel = 0
        self.overLabel = 0
        self.overButton = 0

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.pluginName = wx.StaticText(self, -1, ' ' + self.getName())
        self.pluginClose = wx.BitmapButton(self, -1, wx.Bitmap('C:\Users\André Ferreira\Desktop\Tese\Código Python\SoundLog\Images\close.png'), style=wx.NO_BORDER)
        self.pluginClose.Hide()

        gs = wx.GridSizer(2, 2, 0, 0)
        gs.AddMany([(self.pluginName, 0, wx.ALIGN_LEFT), (self.pluginClose, 0, wx.ALIGN_RIGHT|wx.CENTER)])

        sizer.Add(gs, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.pluginName.Bind(wx.EVT_LEAVE_WINDOW, self.onLabelMouseLeave)
        self.pluginName.Bind(wx.EVT_ENTER_WINDOW, self.onLabelMouseOver)
        self.pluginClose.Bind(wx.EVT_BUTTON, self.onCloseMouseClick)
        self.Bind(wx.EVT_LEAVE_WINDOW, self.onPanelMouseLeave)
        self.Bind(wx.EVT_ENTER_WINDOW, self.onPanelMouseOver)

    def onPanelMouseOver(self, event):
        self.overPanel = 1
        self.overLabel = 0
        self.SetBackgroundColour(self.colorOver)
        self.pluginName.SetForegroundColour(self.colorFont)
        self.Refresh()
        self.pluginClose.Show()

    def onPanelMouseLeave(self, event):
        if self.overLabel == 0:
            self.overPanel = 0
            self.SetBackgroundColour(self.colorLeave)
            self.pluginName.SetForegroundColour(self.colorLeave)
            self.Refresh()
            self.pluginClose.Hide()

    def onLabelMouseOver(self, event):
        self.overPanel = 0
        self.overLabel = 1
        self.SetBackgroundColour(self.colorOver)
        self.pluginName.SetForegroundColour(self.colorFont)
        self.Refresh()
        self.pluginClose.Show()

    def onLabelMouseLeave(self, event):
        if self.overPanel == 0:
            self.overLabel = 0
            self.SetBackgroundColour(self.colorLeave)
            self.pluginName.SetForegroundColour(self.colorLeave)
            self.Refresh()
            self.pluginClose.Hide()

    def OnClose(self, event):
        self.Close()
        app.Destroy()

    def onCloseMouseClick(self, event):
        self.Hide()

    def getName(self):
        return self.name

每当我越过 BitmapButton 时,接下来的两个事件:

self.Bind(wx.EVT_LEAVE_WINDOW, self.onPanelMouseLeave)
self.Bind(wx.EVT_ENTER_WINDOW, self.onPanelMouseOver)

总是被调用。

这有什么问题吗?怎么只能调用一个事件呢?

注意:如果我将离开和进入窗口事件绑定到 BitmapButton,则仍会调用前面的两个事件。

with this code:

import wx


class Plugin(wx.Panel):

    def __init__(self, parent, *args, **kwargs):
        panel = wx.Panel.__init__(self, parent, *args, **kwargs)
        self.colorOver = ((89,89,89))
        self.colorLeave = ((110,110,110))
        self.colorFont = ((145,145,145))
        self.SetBackgroundColour(self.colorLeave)
        self.SetForegroundColour(self.colorLeave)
        self.name = "Plugin"
        self.overPanel = 0
        self.overLabel = 0
        self.overButton = 0

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.pluginName = wx.StaticText(self, -1, ' ' + self.getName())
        self.pluginClose = wx.BitmapButton(self, -1, wx.Bitmap('C:\Users\André Ferreira\Desktop\Tese\Código Python\SoundLog\Images\close.png'), style=wx.NO_BORDER)
        self.pluginClose.Hide()

        gs = wx.GridSizer(2, 2, 0, 0)
        gs.AddMany([(self.pluginName, 0, wx.ALIGN_LEFT), (self.pluginClose, 0, wx.ALIGN_RIGHT|wx.CENTER)])

        sizer.Add(gs, 1, wx.EXPAND)
        self.SetSizer(sizer)

        self.pluginName.Bind(wx.EVT_LEAVE_WINDOW, self.onLabelMouseLeave)
        self.pluginName.Bind(wx.EVT_ENTER_WINDOW, self.onLabelMouseOver)
        self.pluginClose.Bind(wx.EVT_BUTTON, self.onCloseMouseClick)
        self.Bind(wx.EVT_LEAVE_WINDOW, self.onPanelMouseLeave)
        self.Bind(wx.EVT_ENTER_WINDOW, self.onPanelMouseOver)

    def onPanelMouseOver(self, event):
        self.overPanel = 1
        self.overLabel = 0
        self.SetBackgroundColour(self.colorOver)
        self.pluginName.SetForegroundColour(self.colorFont)
        self.Refresh()
        self.pluginClose.Show()

    def onPanelMouseLeave(self, event):
        if self.overLabel == 0:
            self.overPanel = 0
            self.SetBackgroundColour(self.colorLeave)
            self.pluginName.SetForegroundColour(self.colorLeave)
            self.Refresh()
            self.pluginClose.Hide()

    def onLabelMouseOver(self, event):
        self.overPanel = 0
        self.overLabel = 1
        self.SetBackgroundColour(self.colorOver)
        self.pluginName.SetForegroundColour(self.colorFont)
        self.Refresh()
        self.pluginClose.Show()

    def onLabelMouseLeave(self, event):
        if self.overPanel == 0:
            self.overLabel = 0
            self.SetBackgroundColour(self.colorLeave)
            self.pluginName.SetForegroundColour(self.colorLeave)
            self.Refresh()
            self.pluginClose.Hide()

    def OnClose(self, event):
        self.Close()
        app.Destroy()

    def onCloseMouseClick(self, event):
        self.Hide()

    def getName(self):
        return self.name

whenever I get over the BitmapButton the next two events:

self.Bind(wx.EVT_LEAVE_WINDOW, self.onPanelMouseLeave)
self.Bind(wx.EVT_ENTER_WINDOW, self.onPanelMouseOver)

are allways beeing called.

What is wrong with it? How can only one event be called?

Note: if I bind the leave and enter window events to the BitmapButton the two previous events are still called.

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

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

发布评论

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

评论(1

青朷 2024-08-29 22:42:24

问题是您在鼠标悬停时隐藏和显示位图,因此当您移动鼠标悬停位图时,鼠标离开面板,调用 onPanelMouseLeave 并隐藏位图,这意味着现在如果鼠标悬停在面板上,则调用 onPanelMouseOver ,在这种情况下您再次显示位图这意味着现在如果再次将鼠标悬停在位图上并移出面板,因此您将陷入隐藏/显示圆圈中。

在onPanelMouseLeave中注释掉self.pluginClose.Hide(),你将不会看到多个事件。

尝试采用不同的方法,例如检查鼠标是否位于位图上,因此不要隐藏它。

Problem is you are hiding and showing bitmap on mouseover, so when you move mouseover bitmap, mouse goes off panel, onPanelMouseLeave is called and you hide bitmap, which means now mouse if over panel, onPanelMouseOver is called and in that event you again show bitmap and that means now mouse if over bitmap again and out of panel and hence you are stuck in a hide/show circle.

Comment out self.pluginClose.Hide() in onPanelMouseLeave and you won't see multiple events.

Try to come with a different approach e.g. check if mouse if over bitmap and hence do not hide it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文