使用wxPython獲取系統(tǒng)剪貼板中的數據的教程
更新時間:2015年05月06日 12:07:37 投稿:goldensun
這篇文章主要介紹了使用wxPython獲取系統(tǒng)剪貼板中的數據的教程,wxPython是一個非常受歡迎的Python圖形庫,需要的朋友可以參考下
涉及到開發(fā)桌面程序,尤其是文本處理,剪貼板就很常用,不像 java 中那么煩鎖,wxpython 中訪問剪貼板非常簡單,寥寥幾句足以。
# 取得剪貼板并確保其為打開狀態(tài) 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 wx class 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()
相關文章
python pandas模糊匹配 讀取Excel后 獲取指定指標的操作
這篇文章主要介紹了python pandas模糊匹配 讀取Excel后 獲取指定指標的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03利用python實現(xiàn)JSON文檔與Python對象互相轉換
這篇文章主要介紹了利用python實現(xiàn)JSON文檔與Python對象互相轉換,通過對將一個JSON文檔映射為Python對象問題的展開介紹主題內容,需要的朋友可以參考一下2022-06-06Python中執(zhí)行MySQL結果限制和分頁查詢示例詳解
這篇文章主要為大家介紹了Python中執(zhí)行MySQL結果限制和分頁查詢示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11