我遇到了wx.EVT_TEXT(CUT,COPY,PASTE)的问题。我不知道为什么,但是当我在Page中按Ctrl + C或Ctrl + X或Ctrl + V时,会调用类Page的EventHandler,但不是按照我的意图调用它,它会多次运行,最后它会运行由于超出最大递归而导致RunTimeError。类的结构是Editor has Notebook,Notebook有Page。使用的Python版本是2.7.1,wxPython ver。 2.8.8.1,OS WinXP SP2。
class Page(wx.TextCtrl):
def __init__(self, parent, Path=''):
super(Notebook.Page, self).__init__(parent, style = wx.TE_MULTILINE)
self.Bind(wx.EVT_TEXT_CUT, self.EventHandler, self)
self.Bind(wx.EVT_TEXT_COPY, self.EventHandler, self)
self.Bind(wx.EVT_TEXT_PASTE, self.EventHandler, self)
def EventHandler(self, Event):
if Event.GetEventType() == 10119:
print 'I\'m called'
Event.Skip()
self.GetGrandParent().EventHandler(wx.ID_CUT)
elif Event.GetEventType() == 10118:
self.GetGrandParent().EventHandler(wx.ID_COPY)
elif Event.GetEventType() == 10120:
self.GetGrandParent().EventHandler(wx.ID_PASTE)
Grand Parent的代码部分就是这个
class Editor(wx.Frame):
def __init__(self, parent, *args, **kwargs):
wx.Frame.__init__(self, parent, *args, **kwargs)
def EventHandler(self, Event):
if type(Event) != int:
EventID = Event.GetId()
EventObj = Event.GetEventObject()
else:
EventID = Event
## Cut or Copy
if EventID == wx.ID_CUT or EventID == wx.ID_COPY:
Page = self.Note.GetPage(self.Note.GetSelection())
print Page
if EventID == wx.ID_CUT:
Page.Cut()
else:
Page.Copy()
if self.Clipboard.IsShown():
if not wx.TheClipboard.IsOpened():
wx.TheClipboard.Open()
Data = wx.TextDataObject()
Success = wx.TheClipboard.GetData(Data)
wx.TheClipboard.Close()
if Success:
self.Clipboard.AddData(Data.GetText())
## Paste
if EventID == wx.ID_PASTE:
if not wx.TheClipboard.IsOpened():
wx.TheClipboard.Open()
Data = wx.TextDataObject(self.Clipboard.ListBox.GetString(
self.Clipboard.ListBox.GetSelection()))
wx.TheClipboard.SetData(Data)
self.Note.GetPage(self.Note.GetSelection()).Paste()
wx.TheClipboard.Close()
告诉我是否需要其他信息。提前谢谢。