本文實例講述了wxPython使用系統剪切板的方法。分享給大家供大家參考。具體如下:
程序運行效果如下圖所示:
主要代碼如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import wx ######################################################################## class ClipboardPanel(wx.Panel): """""" #---------------------------------------------------------------------- def __init__(self, parent): """Constructor""" wx.Panel.__init__(self, parent) lbl = wx.StaticText(self, label="Enter text to copy to clipboard:") self.text = wx.TextCtrl(self, style=wx.TE_MULTILINE) copyBtn = wx.Button(self, label="Copy") copyBtn.Bind(wx.EVT_BUTTON, self.onCopy) copyFlushBtn = wx.Button(self, label="Copy and Flush") copyFlushBtn.Bind(wx.EVT_BUTTON, self.onCopyAndFlush) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(lbl, 0, wx.ALL, 5) sizer.Add(self.text, 1, wx.EXPAND) sizer.Add(copyBtn, 0, wx.ALL|wx.CENTER, 5) sizer.Add(copyFlushBtn, 0, wx.ALL|wx.CENTER, 5) self.SetSizer(sizer) #---------------------------------------------------------------------- def onCopy(self, event): """""" self.dataObj = wx.TextDataObject() self.dataObj.SetText(self.text.GetValue()) if wx.TheClipboard.Open(): wx.TheClipboard.SetData(self.dataObj) wx.TheClipboard.Close() else: wx.MessageBox("Unable to open the clipboard", "Error") #---------------------------------------------------------------------- def onCopyAndFlush(self, event): """""" self.dataObj = wx.TextDataObject() self.dataObj.SetText(self.text.GetValue()) if wx.TheClipboard.Open(): wx.TheClipboard.SetData(self.dataObj) wx.TheClipboard.Flush() else: wx.MessageBox("Unable to open the clipboard", "Error") self.GetParent().Close() ######################################################################## class ClipboardFrame(wx.Frame): """""" #---------------------------------------------------------------------- def __init__(self): """Constructor""" wx.Frame.__init__(self, None, title="Clipboard Tutorial") panel = ClipboardPanel(self) self.Show() if __name__ == "__main__": app = wx.App(False) frame = ClipboardFrame() app.MainLoop()希望本文所述對大家的Python程序設計有所幫助。