使用wxPython擷取系統剪貼簿中的資料的教程

來源:互聯網
上載者:User
涉及到開發傳統型程式,尤其是文本處理,剪貼簿就很常用,不像 java 中那麼煩鎖,wxpython 中訪問剪貼簿非常簡單,寥寥幾句足以。

# 取得剪貼簿並確保其為開啟狀態text_obj = wx.TextDataObject()wx.TheClipboard.Open()if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():  # do something...  wx.TheClipboard.Close()

取值:

if wx.TheClipboard.GetData(text_obj):  text = text_obj.GetText()

寫值:

text_obj.SetText(‘要寫入的值')wx.TheClipboard.SetData(text_obj)

下面的例子中,點擊 Copy 會將文字框中的值複製到剪貼簿,點擊 Paste 會將剪貼簿中的文本粘貼到文字框中。

"""Get text from and put text on the clipboard."""import wxclass MyFrame(wx.Frame):  def __init__(self):    wx.Frame.__init__(self, None, title='Accessing the clipboard', size=(400, 300))    # Components    self.panel = wx.Panel(self)    self.text = wx.TextCtrl(self.panel, pos=(10, 10), size=(370, 220))    self.copy = wx.Button(self.panel, wx.ID_ANY, label='Copy', pos=(10, 240))    self.paste = wx.Button(self.panel, wx.ID_ANY, label='Paste', pos=(100, 240))    # Event bindings.    self.Bind(wx.EVT_BUTTON, self.OnCopy, self.copy)    self.Bind(wx.EVT_BUTTON, self.OnPaste, self.paste)  def OnCopy(self, event):    text_obj = wx.TextDataObject()    text_obj.SetText(self.text.GetValue())    if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():      wx.TheClipboard.SetData(text_obj)      wx.TheClipboard.Close()  def OnPaste(self, event):    text_obj = wx.TextDataObject()    if wx.TheClipboard.IsOpened() or wx.TheClipboard.Open():      if wx.TheClipboard.GetData(text_obj):        self.text.SetValue(text_obj.GetText())      wx.TheClipboard.Close()app = wx.App(False)frame = MyFrame()frame.Show(True)app.MainLoop()
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.