使用wxPython获取系统剪贴板中的数据的教程
涉及到开发桌面程序,尤其是文本处理,剪贴板就很常用,不像java中那么烦锁,wxpython中访问剪贴板非常简单,寥寥几句足以。
#取得剪贴板并确保其为打开状态 text_obj=wx.TextDataObject() wx.TheClipboard.Open() ifwx.TheClipboard.IsOpened()orwx.TheClipboard.Open(): #dosomething... wx.TheClipboard.Close()
取值:
ifwx.TheClipboard.GetData(text_obj): text=text_obj.GetText()
写值:
text_obj.SetText(‘要写入的值') wx.TheClipboard.SetData(text_obj)
下面的例子中,点击Copy会将文本框中的值复制到剪贴板,点击Paste会将剪贴板中的文本粘贴到文本框中。
""" Gettextfromandputtextontheclipboard. """ importwx classMyFrame(wx.Frame): def__init__(self): wx.Frame.__init__(self,None,title='Accessingtheclipboard',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)) #Eventbindings. self.Bind(wx.EVT_BUTTON,self.OnCopy,self.copy) self.Bind(wx.EVT_BUTTON,self.OnPaste,self.paste) defOnCopy(self,event): text_obj=wx.TextDataObject() text_obj.SetText(self.text.GetValue()) ifwx.TheClipboard.IsOpened()orwx.TheClipboard.Open(): wx.TheClipboard.SetData(text_obj) wx.TheClipboard.Close() defOnPaste(self,event): text_obj=wx.TextDataObject() ifwx.TheClipboard.IsOpened()orwx.TheClipboard.Open(): ifwx.TheClipboard.GetData(text_obj): self.text.SetValue(text_obj.GetText()) wx.TheClipboard.Close() app=wx.App(False) frame=MyFrame() frame.Show(True) app.MainLoop()
热门推荐
10 诗词送行祝福语大全简短
11 新房开工吉日祝福语简短
12 50多岁生日简短祝福语
13 安徽疫情祝福语简短英语
14 农民朋友发财祝福语简短
15 对生活祝福语简短精辟
16 搬家词简短祝福语朋友
17 女神结婚快乐祝福语简短
18 文学短句祝福语大全简短