本文实例讲述了wxpython使用系统剪切板的方法。分享给大家供大家参考。具体如下:
程序运行效果如下图所示:
主要代码如下:
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程序设计有所帮助。