2015-06-19 65 views
1

我创建一个wx.PyValidator我的文本框:wxPython的 - 实施wx.EVT_TEXT_PASTE

class IPValidator(wx.PyValidator): 
    "Validator for validating IP addresses" 

    def __init__(self): 
     super(IPValidator, self).__init__() 
     self.Bind(wx.EVT_TEXT_PASTE, self.OnPaste) 

    def Clone(self): 
     """Cloning the validator""" 
     return self.__class__() 

    def Validate(self, win): 
     """the validate function""" 
     return self.OnValidate(self.GetWindow().GetValue()) 

    def OnValidate(self, text): 
     """returns True or False about the given text""" 
     return re.match(text, ip_pattern) 

    def OnPaste(self, event): ####### 
     text = event.GetString() 
     if self.OnValidate(text): 
      event.Skip() 

    def TransferToWindow(self): 
     return True 

    def TransferFromWindow(self): 
     return True 

我在OnPaste方法的问题。我怎样才能得到被粘贴的字符串,并确保它是有效的? event.GetString返回一个空字符串

回答

0

您需要的接口剪贴板直接

def get_clipboard_text(): 
    if not wx.TheClipboard.IsOpened(): wx.TheClipboard.Open() 
    do = wx.TextDataObject() 
    success = wx.TheClipboard.GetData(do) 
    if success: 
     return do.GetText() 
    return "" 

class MyWidget(...): 
    .... 
    def OnPaste(self, event): ####### 
     text = get_clipboard_text() 
     if self.OnValidate(text): 
      event.Skip() 

是你怎么可以只用WX做到这一点......但我建议使用类似pyperclip,因为它是很容易对付