圆月山庄资源网 Design By www.vgjia.com

本文实例讲述了wxPython使用系统剪切板的方法。分享给大家供大家参考。具体如下:

程序运行效果如下图所示:

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程序设计有所帮助。

标签:
wxPython,系统,剪切板

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com