【问题标题】:How to fix the wxpython events called?如何修复调用的 wxpython 事件?
【发布时间】:2011-01-19 08:16:48
【问题描述】:

使用此代码:

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 之前的两个事件仍然会被调用。

【问题讨论】:

    标签: python events button wxpython panel


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2017-01-07
      • 2010-10-19
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多