我想提供一個自定義按鈕類,需要懸停才能正常工作。因此我使用了StaticBitmap並對其進行了擴展。但是我發現Windows和Linux之間使用相同的確切代碼之間的差異。下面的小例子在Linux中不能正常工作,但在Windows中正常工作:wxPython鼠標輸入/離開事件方法在Linux上不起作用
import wx
class MyForm(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test")
panel = wx.Panel(self, wx.ID_ANY, size=(200,100))
panel.SetBackgroundColour("Black")
# create a normal bitmap button
bitmap = wx.Bitmap("images/power.png", wx.BITMAP_TYPE_ANY)
self.image1 = wx.StaticBitmap(panel, bitmap=bitmap, size=bitmap.GetSize())
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.image1)
panel.SetSizer(sizer)
# This works on Windows, but not on Linux
#self.image1.Bind(wx.EVT_ENTER_WINDOW, self.OnHover)
#self.image1.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveHover)
# This (used by itself) gets events for both Linux/Win, but
# doesn't find e.Entering() or e.Leaving() on Linux!
self.image1.Bind(wx.EVT_MOUSE_EVENTS, self.OnMouseEvents)
def OnHover(self, e):
print "Hover"
def OnLeaveHover(self,e):
print "Leaving Hover"
def OnMouseEvents(self,e):
print "Mouse event"
#print dir(e)
if e.Entering():
print "Hover"
elif e.Leaving():
print "Leaving Hover"
else:
e.Skip()
if __name__ == "__main__":
app = wx.App(False)
frame = MyForm()
frame.Show()
app.MainLoop()
這是已知問題嗎?有沒有其他的方式來綁定事件,讓鼠標懸停/懸停操作在Linux中正常工作?
此外,沒有任何形式的self.Bind()
在Linux或Windows上工作。爲什麼?即:
self.Bind(wx.EVT_ENTER_WINDOW, self.OnHover, self.image1)
self.Bind(wx.EVT_LEAVE_WINDOW, self.OnLeaveHover, self.image1)
約self.Bind第二個問題()不工作的Linux或Windows中「的wxPython在行動」一書進行了說明。輸入/離開事件不能綁定到框架。所以從頭開始。更大的問題是爲什麼Entering()/ Leaving()在Linux上不起作用。 – 2013-02-18 05:06:04